Difference between $@ and $* in shell scripting

Morning members

Under shell scripting, what is the difference main between $@ and $* when dealing with positional parameters?
The two variables are always confusing especially when it comes to getting the total number of arguments passed to a script in terms of choosing which one to use in a shell script.

Thanks

The difference comes in how they are expanded.

$* expands to a single argument with all the elements delimited by spaces (actually the first character of $IFS).
$@ expands to multiple arguments.


For example


#!/bin/bash
echo “With :"
for arg in "$
”; do echo “<$arg>”; done
echo
echo “With @:”
for arg in “$@”; do echo “<$arg>”; done

$ /tmp/test.sh 1 2 “3 4”
With *:
<1 2 3 4>

With @:
<1>
<2>
<3 4>

1 Like

@tux2112 Thanks, clear and precise explanation…