8

What does $* mean in shell?

I have code two function, run_command and run_test, they are called like this:

if [ -n "`echo ${GOAT_COMMANDS} | grep $1`" ]; then
  run_command $*
else
  run_test $*
fi

I want to understand what $* means, but Google search is very bad for searching these kinds of things.

polym
  • 10,672
  • 9
  • 41
  • 65
betteroutthanin
  • 580
  • 4
  • 8
  • 15
  • You may want to use [duckduckgo.com](https://duckduckgo.com/?q=what+does+%24*+mean+in+shell&atb=v92-6__&ia=qa) to search for special characters – Ricardo May 10 '18 at 22:57

4 Answers4

15

$* is all the parameters to the script or function, split into separate arguments on whitespace.

If you have a function:

foo() {
    for x in $*
    do
        echo $x
    done
}

then running foo 1 "2 3 4" 5 will output:

1
2
3
4
5

That is, the quoted string "2 3 4" will be split into three separate "words".

When you don't want to split the arguments into separate words like that, you can use $@. If the same function used "$@" in place of $*, it would output:

1
2 3 4
5

That is, the quoted string would not be split into separate words. This is like listing all the positional parameters out explicitly: "$1" "$2" "$3" "$4" .... In general, this is probably the one you want to use - the word-splitting will often lead to nasty bugs that are hard to track down.

At the top level of a script (not inside a function), these refer to the arguments given to the script itself.

Your example is calling either run_command or run_test with all of the arguments to this script or function, split into separate words. If the script were run as script.sh "hello world" 2 3 then it would have the effect of running:

run_command hello world 2 3
Michael Homer
  • 74,824
  • 17
  • 212
  • 233
  • Some related [related reading material](http://www.tldp.org/LDP/abs/html/internalvariables.html) for the OP. – HalosGhost Jul 08 '14 at 08:31
  • Does `$*` remove the `"`? I.e. does the first version output `1,2,3,4,5`, and not `1,"2,3,4",5`? – Svish Jul 08 '14 at 09:57
  • @Svish: Quotes `"2 3 4"` are [removed by the shell right at the start](https://www.gnu.org/software/bash/manual/bashref.html#Double-Quotes) - they make the whole thing a single "word", rather than splitting it into three the way it usually would. – Michael Homer Jul 08 '14 at 10:03
3

$* means pass all parameters to the function as a single 'parameter'. It's also advised to have double quotes around such variable "$*".

Some doc on this here section 3.2.5

On the other hand, if you need to iterate through each one of the parameters contained in the argument list, use $@ instead, like in this ex:

for var in "$@"
do
    echo "$var"
done

Here's another example: foo.sh

#!/bin/bash    
foo1()
{
        for x in "$@"
        do
                echo "f1: $x"
        done
}

foo2()
{
        for x in "$*"
        do
                echo "f2: $x"
        done
}

foo1 1 2 "3 4 5" 6 7
echo
foo2 1 2 "3 4 5" 6 7

output:

$  ./foo.sh 
f1: 1
f1: 2
f1: 3 4 5
f1: 6
f1: 7

f2: 1 2 3 4 5 6 7
fduff
  • 4,925
  • 4
  • 35
  • 39
  • 1
    `$*` passes the parameters as a single parameter **only** when you use double quotes: `"$*"`. I believe that's the main use of `$*`, with double quotes. Otherwise, for a list of parameters, `"$@"` is chosen. – vinc17 Jul 08 '14 at 08:38
2

$* means the all positional parameters.

polym
  • 10,672
  • 9
  • 41
  • 65
2

$* is the list of the positional parameters, but with word splitting. For instance, on

your_script ab "cd ef"

$* will give you 3 arguments: ab, cd and ef. Using "$@" is much safer (unless you want word splitting).

vinc17
  • 11,912
  • 38
  • 45