What does $!# mean in Shell scripting? [duplicate]How does $!# work in bash to get the last command-line argument?Functional shell scriptingCounters in shellShell Script Size 57Mcronjob bash script called within another shell script not workingWait for key in shell script that may get piped to /bin/bashwhat could be the reasons for shell scripts kill itself?Shell script work on terminal, not when it has run by cronjobCan we write a script to run some command within each interactive bash shell process?How can I skip the rest of a script without exiting the invoking shell, when sourcing the script?What exit modes exist in shell-scripting in general and in Bash in particular?
What are the problems in teaching guitar via Skype?
Select row of data if next row contains zero
What is the intuition behind uniform continuity?
Looking after a wayward brother in mother's will
What caused the tendency for conservatives to not support climate change regulations?
What is the difference between nullifying your vote and not going to vote at all?
Modern approach to radio buttons
Is it possible to kill all life on Earth?
Thousands and thousands of words
Expenditure in Poland - Forex doesn't have Zloty
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
common answer to a question about a price for alcohol
What is/are this/these giant NASA box(es)?
Points within polygons in different projections
Creating Fictional Slavic Place Names
Can you move on your turn, and then use the Ready Action to move again on another creature's turn?
The deliberate use of misleading terminology
Can a helicopter mask itself from Radar?
Why to use water tanks from Space shuttle in museum?
Future enhancements for the finite element method
Can a wire having a 610-670 THz (frequency of blue light) AC frequency supply, generate blue light?
Can I install a row of bricks on a slab to support a shed?
If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?
Uncommanded roll at high speed
What does $!# mean in Shell scripting? [duplicate]
How does $!# work in bash to get the last command-line argument?Functional shell scriptingCounters in shellShell Script Size 57Mcronjob bash script called within another shell script not workingWait for key in shell script that may get piped to /bin/bashwhat could be the reasons for shell scripts kill itself?Shell script work on terminal, not when it has run by cronjobCan we write a script to run some command within each interactive bash shell process?How can I skip the rest of a script without exiting the invoking shell, when sourcing the script?What exit modes exist in shell-scripting in general and in Bash in particular?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
How does $!# work in bash to get the last command-line argument?
1 answer
Found this in an obfuscated malicious shell script, beginning with:
$!#$*^ <<<
...
Could not find any reference to $!#
, but when echo
'd, it outputs -bash
. Is this a secret referene to the running shell? Why there is an extra dash then?
Thanks!
shell-script
New contributor
marked as duplicate by Jesse_b, 200_success, X Tian, garethTheRed, Philip Couling May 24 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How does $!# work in bash to get the last command-line argument?
1 answer
Found this in an obfuscated malicious shell script, beginning with:
$!#$*^ <<<
...
Could not find any reference to $!#
, but when echo
'd, it outputs -bash
. Is this a secret referene to the running shell? Why there is an extra dash then?
Thanks!
shell-script
New contributor
marked as duplicate by Jesse_b, 200_success, X Tian, garethTheRed, Philip Couling May 24 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
In "shell scripting", that will expand to the PID of the last background command. Inbash
(and only inbash
),$!#
is the last positional parameter obtained via "Indirect Expansion" (the$!var
syntax) of the the$#
variable.
– mosvy
May 23 at 14:07
4
@mosvy That looks like an answer, not a clarification of the question. As you point out, it includes information missing from the current answer, which is exactly why the system allows multiple answers to one question.
– IMSoP
May 23 at 14:31
Regarding the extra dash itself, refer to this answer: superuser.com/a/278865/560377
– Phlarx
May 23 at 16:27
add a comment |
This question already has an answer here:
How does $!# work in bash to get the last command-line argument?
1 answer
Found this in an obfuscated malicious shell script, beginning with:
$!#$*^ <<<
...
Could not find any reference to $!#
, but when echo
'd, it outputs -bash
. Is this a secret referene to the running shell? Why there is an extra dash then?
Thanks!
shell-script
New contributor
This question already has an answer here:
How does $!# work in bash to get the last command-line argument?
1 answer
Found this in an obfuscated malicious shell script, beginning with:
$!#$*^ <<<
...
Could not find any reference to $!#
, but when echo
'd, it outputs -bash
. Is this a secret referene to the running shell? Why there is an extra dash then?
Thanks!
This question already has an answer here:
How does $!# work in bash to get the last command-line argument?
1 answer
shell-script
shell-script
New contributor
New contributor
New contributor
asked May 23 at 2:35
DaiDai
23316
23316
New contributor
New contributor
marked as duplicate by Jesse_b, 200_success, X Tian, garethTheRed, Philip Couling May 24 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jesse_b, 200_success, X Tian, garethTheRed, Philip Couling May 24 at 14:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
In "shell scripting", that will expand to the PID of the last background command. Inbash
(and only inbash
),$!#
is the last positional parameter obtained via "Indirect Expansion" (the$!var
syntax) of the the$#
variable.
– mosvy
May 23 at 14:07
4
@mosvy That looks like an answer, not a clarification of the question. As you point out, it includes information missing from the current answer, which is exactly why the system allows multiple answers to one question.
– IMSoP
May 23 at 14:31
Regarding the extra dash itself, refer to this answer: superuser.com/a/278865/560377
– Phlarx
May 23 at 16:27
add a comment |
3
In "shell scripting", that will expand to the PID of the last background command. Inbash
(and only inbash
),$!#
is the last positional parameter obtained via "Indirect Expansion" (the$!var
syntax) of the the$#
variable.
– mosvy
May 23 at 14:07
4
@mosvy That looks like an answer, not a clarification of the question. As you point out, it includes information missing from the current answer, which is exactly why the system allows multiple answers to one question.
– IMSoP
May 23 at 14:31
Regarding the extra dash itself, refer to this answer: superuser.com/a/278865/560377
– Phlarx
May 23 at 16:27
3
3
In "shell scripting", that will expand to the PID of the last background command. In
bash
(and only in bash
), $!#
is the last positional parameter obtained via "Indirect Expansion" (the $!var
syntax) of the the $#
variable.– mosvy
May 23 at 14:07
In "shell scripting", that will expand to the PID of the last background command. In
bash
(and only in bash
), $!#
is the last positional parameter obtained via "Indirect Expansion" (the $!var
syntax) of the the $#
variable.– mosvy
May 23 at 14:07
4
4
@mosvy That looks like an answer, not a clarification of the question. As you point out, it includes information missing from the current answer, which is exactly why the system allows multiple answers to one question.
– IMSoP
May 23 at 14:31
@mosvy That looks like an answer, not a clarification of the question. As you point out, it includes information missing from the current answer, which is exactly why the system allows multiple answers to one question.
– IMSoP
May 23 at 14:31
Regarding the extra dash itself, refer to this answer: superuser.com/a/278865/560377
– Phlarx
May 23 at 16:27
Regarding the extra dash itself, refer to this answer: superuser.com/a/278865/560377
– Phlarx
May 23 at 16:27
add a comment |
1 Answer
1
active
oldest
votes
This answer is bash
-specific, because you tried the echo
in bash
. Not all shells behave the same.
In bash
, if $var1
is foo
, then $!var1
is the same as $foo
. The !
is indirect expansion: it causes bash
to retrieve from the given variable a variable name (foo
) instead of a value.
Now replace var1
with #
. $#
is the number of arguments.
If $#
is 0, $!#
is $0
.
If $#
is 4, $!#
is $4
.
In other words, $!#
is the last positional parameter, no matter how many there positional parameters there are.
If there are no positional parameters, $#
is 0
, so the result is $0
, which is the name of the shell or script (reference). In your case, that was bash
(plus a leading -
meaning that it's a login shell).
Quick test:
$ echo $!#
-bash
$ set the quick brown fox
$ echo $!#
fox
19
Your answer only applies tobash
, but you fail to mention that. In all the other shells,$!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the$var#pat
form of parameter expansions).
– mosvy
May 23 at 13:55
1
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This answer is bash
-specific, because you tried the echo
in bash
. Not all shells behave the same.
In bash
, if $var1
is foo
, then $!var1
is the same as $foo
. The !
is indirect expansion: it causes bash
to retrieve from the given variable a variable name (foo
) instead of a value.
Now replace var1
with #
. $#
is the number of arguments.
If $#
is 0, $!#
is $0
.
If $#
is 4, $!#
is $4
.
In other words, $!#
is the last positional parameter, no matter how many there positional parameters there are.
If there are no positional parameters, $#
is 0
, so the result is $0
, which is the name of the shell or script (reference). In your case, that was bash
(plus a leading -
meaning that it's a login shell).
Quick test:
$ echo $!#
-bash
$ set the quick brown fox
$ echo $!#
fox
19
Your answer only applies tobash
, but you fail to mention that. In all the other shells,$!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the$var#pat
form of parameter expansions).
– mosvy
May 23 at 13:55
1
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
add a comment |
This answer is bash
-specific, because you tried the echo
in bash
. Not all shells behave the same.
In bash
, if $var1
is foo
, then $!var1
is the same as $foo
. The !
is indirect expansion: it causes bash
to retrieve from the given variable a variable name (foo
) instead of a value.
Now replace var1
with #
. $#
is the number of arguments.
If $#
is 0, $!#
is $0
.
If $#
is 4, $!#
is $4
.
In other words, $!#
is the last positional parameter, no matter how many there positional parameters there are.
If there are no positional parameters, $#
is 0
, so the result is $0
, which is the name of the shell or script (reference). In your case, that was bash
(plus a leading -
meaning that it's a login shell).
Quick test:
$ echo $!#
-bash
$ set the quick brown fox
$ echo $!#
fox
19
Your answer only applies tobash
, but you fail to mention that. In all the other shells,$!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the$var#pat
form of parameter expansions).
– mosvy
May 23 at 13:55
1
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
add a comment |
This answer is bash
-specific, because you tried the echo
in bash
. Not all shells behave the same.
In bash
, if $var1
is foo
, then $!var1
is the same as $foo
. The !
is indirect expansion: it causes bash
to retrieve from the given variable a variable name (foo
) instead of a value.
Now replace var1
with #
. $#
is the number of arguments.
If $#
is 0, $!#
is $0
.
If $#
is 4, $!#
is $4
.
In other words, $!#
is the last positional parameter, no matter how many there positional parameters there are.
If there are no positional parameters, $#
is 0
, so the result is $0
, which is the name of the shell or script (reference). In your case, that was bash
(plus a leading -
meaning that it's a login shell).
Quick test:
$ echo $!#
-bash
$ set the quick brown fox
$ echo $!#
fox
This answer is bash
-specific, because you tried the echo
in bash
. Not all shells behave the same.
In bash
, if $var1
is foo
, then $!var1
is the same as $foo
. The !
is indirect expansion: it causes bash
to retrieve from the given variable a variable name (foo
) instead of a value.
Now replace var1
with #
. $#
is the number of arguments.
If $#
is 0, $!#
is $0
.
If $#
is 4, $!#
is $4
.
In other words, $!#
is the last positional parameter, no matter how many there positional parameters there are.
If there are no positional parameters, $#
is 0
, so the result is $0
, which is the name of the shell or script (reference). In your case, that was bash
(plus a leading -
meaning that it's a login shell).
Quick test:
$ echo $!#
-bash
$ set the quick brown fox
$ echo $!#
fox
edited May 24 at 20:01
cxw
95111013
95111013
answered May 23 at 2:51
G-ManG-Man
14.5k94174
14.5k94174
19
Your answer only applies tobash
, but you fail to mention that. In all the other shells,$!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the$var#pat
form of parameter expansions).
– mosvy
May 23 at 13:55
1
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
add a comment |
19
Your answer only applies tobash
, but you fail to mention that. In all the other shells,$!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the$var#pat
form of parameter expansions).
– mosvy
May 23 at 13:55
1
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
19
19
Your answer only applies to
bash
, but you fail to mention that. In all the other shells, $!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the $var#pat
form of parameter expansions).– mosvy
May 23 at 13:55
Your answer only applies to
bash
, but you fail to mention that. In all the other shells, $!#
will expand to the PID of the last background command ($!
) with a empty string removed from the beginning (the $var#pat
form of parameter expansions).– mosvy
May 23 at 13:55
1
1
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
Comments are indeed for clarifying answers; while it's possible to assume bash, given the text in the question, it's certainly reasonable to (collaboratively!) edit this answer to indicate shell-specific behavior.
– Jeff Schaller♦
May 23 at 17:58
add a comment |
3
In "shell scripting", that will expand to the PID of the last background command. In
bash
(and only inbash
),$!#
is the last positional parameter obtained via "Indirect Expansion" (the$!var
syntax) of the the$#
variable.– mosvy
May 23 at 14:07
4
@mosvy That looks like an answer, not a clarification of the question. As you point out, it includes information missing from the current answer, which is exactly why the system allows multiple answers to one question.
– IMSoP
May 23 at 14:31
Regarding the extra dash itself, refer to this answer: superuser.com/a/278865/560377
– Phlarx
May 23 at 16:27