Reading command line arguments

Bash:
Reading command line arguments

How to:

#!/bin/bash

# Print the name of the script.
echo "Script name: $0"

# Print the first argument.
echo "First argument: $1"

# Print all arguments.
echo "All arguments: $@"

Sample output assuming your script is named ’example.sh’ and you call ./example.sh arg1 arg2:

Script name: ./example.sh
First argument: arg1
All arguments: arg1 arg2

Loop through arguments:

#!/bin/bash

# Loop through each argument.
for arg in "$@"; do
  echo "Argument: $arg"
done

Deep Dive

Bash has supported command line arguments for ages; they are positional parameters, $0 to $9, with $@ and $* showing all. $0 is the script itself, $1 to $9 are the first to ninth argument; braces like ${10} are needed from tenth on.

Using $@ is usually better than $* as it handles arguments containing spaces correctly. $@ gives each argument as a separate “word”; $* combines them all into a single “word”.

You can shift through arguments using the shift command, which bumps $2 to $1, and so forth, discarding the old $1.

Alternatives? Sure. getopts and getopt provide more control for options (like -h for help) and flag parsing; check them out if $1, $2,… don’t cut it.

See Also