What do the following environment variables in Linux mean?

  1. What is $* (dollar sign followed by an asterisk)?
  2. What is $# (dollar sign next to a hash mark/number sign/octothorpe/pound sign)?
link|improve this question
feedback

1 Answer

From http://linuxsig.org/files/bash_scripting.html :

$#    Stores the number of command-line arguments that 
      were passed to the shell program.
$?    Stores the exit value of the last command that was 
      executed.
$0    Stores the first word of the entered command (the 
      name of the shell program).
$*    Stores all the arguments that were entered on the
      command line ($1 $2 ...).
"$@"  Stores all the arguments that were entered
      on the command line, individually quoted ("$1" "$2" ...).

So basically, $# is number of arguments given when your script was executed. $* is long string containing all arguments. For example $1 is first argument and so on (if you have to access specific argument in your script).

As Brian commented, there is simple example. If you run following command:

./command -yes -no /home/username

$# should be 3. $* would be "-yes -no /home/username" and $@ is an array containing

{"-yes", "-no", "/home/username"}.

As Dennis noted in comments, this assumes any bash variant as your shell.

link|improve this answer
for example, if you have "./command -yes -no /home/username" $# should be 3. $* would be "-yes -no /home/username" $@ is an array, {"-yes", "-no", "/home/username"} – Brian Feb 17 '11 at 19:06
Those special parameters are true in all Bourne-derived shells (e.g. sh, bash, dash, ash, ksh, zsh). – Dennis Williamson Feb 17 '11 at 19:10
Correction: $* and $# are 1) shell variables, not environment variables; 2) standardized (part of the POSIX spec.) – grawity Feb 17 '11 at 19:11
Also notice that "$@" many times is written as ${1+"$@"} (for portability reasons), see stackoverflow.com/questions/743454/… for an explanation. – hlovdal Feb 17 '11 at 19:55
feedback

Your Answer

 
or
required, but never shown

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