typestar

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

  1. $# counts; "$@" preserves each argument as one word.
  2. shift discards $1 and renumbers the rest.
  3. $* joins everything into a single string — different on purpose.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Functions, step 15 of 26 in Language basics.

← Previous Next →