What does $*
exactly mean in a shell script?
For example consider the following code snippet
$JAVA_HOME/bin/java/com/test/Testclass $*
It's a space separated string of all arguments. For example, if $1 is "hello" and $2 is "world", then $* is "hello world".
"$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...). So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments. For example, $1 is the first argument and so on.
If a script receives two arguments, $* is equivalent to $1 $2. All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
If the $0 special variable is used within a Bash script, it can be used to print its name and if it is used directly within the terminal, it can be used to display the name of the current shell.
It means all the arguments passed to the script or function, split by word.
It is usually wrong and should be replaced by "$@"
, which separates the arguments properly.
It's easy to find answer by yourself: man bash
→ /\$\*
:
Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the
IFS
special variable. That is,"$*"
is equivalent to"$1c$2c..."
, wherec
is the first character of the value of theIFS
variable. IfIFS
is unset, the parameters are separated by spaces. IfIFS
is null, the parameters are joined without intervening separators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With