11

What does $* exactly mean in a shell script?

For example consider the following code snippet

$JAVA_HOME/bin/java/com/test/Testclass $*
Subin_Learner
  • 277
  • 1
  • 4
  • 14
  • 1
    http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST – Nir Alfasi Sep 13 '12 at 20:00
  • All of the positional parameters from the command line calling the script: http://tldp.org/LDP/abs/html/internalvariables.html#APPREF – GreenMatt Sep 13 '12 at 20:02
  • The docs based on POSIX are particularly useful here as they apply to more than just bash: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02 – Ryan Graham May 09 '16 at 16:45

3 Answers3

9

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.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • 5
    Well, @ДМИТРИЙ МАЛИКОВ (Dmitri?) already explained how $* is initially constructed as a single string. The important thing here is that it is subsequently subjected to the shell's normal word splitting, *unless* it is quoted. "$*" will make sure that the argument is still processed as one single, long string. "$@" forces the argument list to be processed as an array of quoted strings. Each has its use, and it is important to know the difference. – Henk Langeveld Sep 13 '12 at 20:26
7

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...", 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.
Community
  • 1
  • 1
4

$* expands to all parameters that were passed to that shell script.

$0 = shell script's name

$1 = first argument

$2 = second argument ...etc

$# = number of arguments passed to shellscript

Ashish Ahuja
  • 4,798
  • 9
  • 48
  • 63
Jin Kim
  • 13,866
  • 16
  • 51
  • 79