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;








26
















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!










share|improve this question







New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










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. 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





    @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


















26
















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!










share|improve this question







New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










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. 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





    @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














26












26








26


3







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!










share|improve this question







New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






share|improve this question







New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question







New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question






New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked May 23 at 2:35









DaiDai

23316




23316




New contributor



Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Dai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






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. 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





    @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





    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





    @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











1 Answer
1






active

oldest

votes


















35














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





share|improve this answer




















  • 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







  • 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

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









35














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





share|improve this answer




















  • 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







  • 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















35














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





share|improve this answer




















  • 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







  • 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













35












35








35







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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 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





    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





    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





    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



Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?