Skip to main content
added 501 characters in body
Source Link
fduff
  • 5.2k
  • 6
  • 40
  • 39

$* 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

$* 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

$* 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
Source Link
fduff
  • 5.2k
  • 6
  • 40
  • 39

$* 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