Script arguments in Bash
What $@, $# and shift do with a command line.
# $@ is all arguments, $# their count, shift discards the first
show_args() {
echo "got $# arguments"
for arg in "$@"; do
echo " arg: $arg"
done
shift
echo "after shift: $*"
}
show_args alpha "two words" gamma
How it works
$#counts;"$@"preserves each argument as one word.shiftdiscards$1and renumbers the rest.$*joins everything into a single string — different on purpose.
Keywords and builtins used here
dodoneechoforinshift
The run, in numbers
- Lines
- 11
- Characters to type
- 211
- Tokens
- 36
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 25 seconds.
Step 3 of 3 in Functions, step 15 of 26 in Language basics.