What is the difference between $* and $@?
- $* returns the number of arguments passed whereas $@ represents the arguments list.
- All arguments are double quoted using $* whereas all arguments are individually double quoted when using $@.
- $* is used in for loop whereas $@ is used in while loop.
- $* and $@ are same
Reveal answer
Fill a bubble to check yourself
B
Correct answer
Explanation
The key difference is in quoting behavior: $* treats all arguments as a single string when quoted ( "$*" expands to "$1 $2 $3..." ), while $@ treats each argument as an individually quoted string ( "\$@" expands to "\$1" "$2" "$3"... ). This matters significantly when passing arguments to commands or iterating through them.
AI explanation
When you use "$*", all the positional parameters are combined into a single string (as if double-quoted together), whereas "$@" expands each positional parameter as its own separately double-quoted word. This distinction matters most when parameters contain spaces — "$@" preserves them as separate arguments while "$*" merges everything into one.