4

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?

$*

$@

$#

11

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.

1
5

The variables $* & $@ are the arguments passed when invoking the script (or function, if inside a function).

"$@" is the one you should normally use: it expands to separate words, one-for-one for the arguments that are passed in.

"$*" expands to a single "word" consisting of all the arguments separated by spaces.

(Don't ever use them without quotes; that results in word-splitting and general messiness you don't want to deal with.)

$# is the number of arguments (and since it's always a digit string, it doesn't really matter whether you quote it)

3

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…", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" …. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing.

# Expands to the number of positional parameters in decimal.

$  Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.

Source: https://www.gnu.org/software/bash/manual/html_node/index.html

0

I'll go through an example to show these three special variables use case:

  • $# number of arguments passed int a variable:

    $ bash -c 'echo number of args to this script are "$#"' _ hello word
    number of args to this script are 2
    
    • our args are hello and word so the "2" is correct
  • $* will be expanded to the all positional parameters like: "$1 $2":

    $ bash -c 'echo -n "args are: "; printf \"%s\" "$*"' _ hello word
    args are: "hello word"
    
    • Note "hello word"
  • $@: Is like an array of all positional parameters "$1", "$2" ...

    $ bash -c 'echo -n "args are: "; printf \"%s\" "$@"' _ hello word
    args are: "hello""word"
    
    • Note "hello" "word"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.