Use this file to discover all available pages before exploring further.
When writing Bash scripts, handling command-line arguments efficiently is crucial. You can access each argument by its position ($1, $2, …), but when the number of parameters varies, special variables like $@ and $* simplify your logic. This guide covers:
Compare two scripts that loop over their arguments:
#!/usr/bin/env bashecho "Number of arguments: $#"echo "All arguments: $@"for arg in "$@"; do echo "Argument: $arg"done
#!/usr/bin/env bashecho "Number of arguments: $#"echo "All arguments: $*"for arg in "$*"; do echo "Argument: $arg"done
$ ./atsign-example1.sh one two threeNumber of arguments: 3All arguments: one two threeArgument: oneArgument: twoArgument: three$ ./star-example1.sh one two threeNumber of arguments: 3All arguments: one two threeArgument: one two three
In the soda‐can analogy:
$@ places each can in its own compartment.
$* pours all the soda into one big bottle—individual cans are no longer separate.
"$*" joins all arguments into a single string, separated by the first character of IFS (default: space).
Unquoted, both behave identically:
#!/usr/bin/env bashprint_section_header() { local title="$1" echo "===============================" echo "= Section ${title} =" echo "==============================="}print_section_header "1: \$@"echo "--> Output of \$@: $@"echo "Looping \$@ without quotes:"for arg in $@; do echo "$arg"doneprint_section_header "2: \$*"echo "--> Output of \$*: $*"echo "Looping \$* without quotes:"for arg in $*; do echo "$arg"done
$ ./special-shell-noquotes.sh one two three================================ Section 1: $@ ================================--> Output of $@: one two threeLooping $@ without quotes:onetwothree================================ Section 2: $* ================================--> Output of $*: one two threeLooping $* without quotes:onetwothree