Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 2456  / 2 Years ago, mon, july 18, 2022, 1:06:35

I know these are few special variables used by bash. But I am not able to understand them.
Can anyone explain what these variables are and how to use them?



$*



$@



$#


More From » bash

 Answers
3

When you call a shell script from the command line, you can pass it additional arguments called Positional Parameters. For example, when entering the following in the command line to run the script myscript.sh:



./myscript.sh param1 param2



The variables would have the following values:



"$0" = "./myscript.sh"
"$1" = "param1"
"$2" = "param2"


The variable $# gives the number of parameters, not including the command. In this example:



"$#" = 2



The variable $* lists all the parameters as a single word (quotes added to emphasize string boundaries):



"$*" = "param1 param2"



The variable $@ lists each parameter as a separate quoted string (quotes added to emphasize string boundaries):



"$@" = "param1" "param2"



Note that you should almost always quote variables in bash, so you should use "$@" and not $@. See this programming guide for more information.


[#23286] Wednesday, July 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aveerakfas

Total Points: 106
Total Questions: 148
Total Answers: 129

Location: Monaco
Member since Sun, Jan 1, 2023
1 Year ago
;