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

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

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

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form