How to Pass Command-Line Parameters to a Shell Script

Updated

Bash shell script in a Linux terminal

Inside a shell script, $1 to $9 are the first nine arguments, ${10} onward need the braces, $# is how many there are, and "$@" is all of them. Use "$@" — with the double quotes — every single time you forward arguments to another command. Every other form silently breaks on an argument containing a space.

The four forms, side by side

This script prints each expansion with every element bracketed, so word splitting is visible:

$ ./show.sh "two words" b "c  d"
$#  = 3
$0  = ./show.sh
$1  = two words
for a in $@   -> [two] [words] [b] [c] [d]
for a in "$@" -> [two words] [b] [c  d]
for a in $*   -> [two] [words] [b] [c] [d]
for a in "$*" -> [two words b c  d]

Describing $@ as “a list of all arguments as separate strings” and $* as “all arguments treated as a single string” is, as the output shows, only true of "$@" and "$*". Unquoted, $@ and $* are the same thing: both expand to the arguments and then get word-split and glob-expanded by the shell, which is why two words became two elements and the two spaces inside c  d vanished. Each bracketed line above, and the zero-argument, ${10}, shift, getopts and BSD getopt results further down, is asserted by how-to-pass-shell-script-arguments.sh, which builds its demo scripts in a temporary directory and removes it on exit; on a machine with GNU getopt it instead tries the success path this page could only quote from the man page.

So the useful rule is not “$@ vs $*“. It is: "$@" preserves argument boundaries, and the other three destroy them. "$*" is occasionally what you want — it joins with the first character of IFS, which makes it handy for logging:

default IFS: [a b c]
IFS=, :      [a,b,c]
IFS= (empty):[abc]

Where it actually breaks

Two scripts, differing only in a pair of quotes, run on a directory containing my report.txt:

$ cat wc_bad.sh
wc -c $@
$ ./wc_bad.sh "my report.txt" notes.txt
wc: my: open: No such file or directory
wc: report.txt: open: No such file or directory
       0 notes.txt

$ cat wc_good.sh
wc -c "$@"
$ ./wc_good.sh "my report.txt" notes.txt
       0 my report.txt
       0 notes.txt

The caller quoted correctly. The script threw that away. This is the failure mode behind most “works on my machine until someone puts a space in a filename” bug reports.

There is a second difference that only shows up with zero arguments. Given no arguments at all, "$@" expands to nothing, while "$*" expands to one empty string:

$ set --            # no arguments
$ f "$@"    ->  inner $#=0
$ f "$*"    ->  inner $#=1

That is why cmd "$@" is safe to write unconditionally, and why cmd "$*" can hand a spurious empty argument to a program that then treats it as an empty filename.

$10 is not the tenth argument

$ ./ten.sh a b c d e f g h i j k
$10   -> a0
${10} -> j
${11} -> k

$10 parses as $1 followed by the literal character 0. Past nine positional parameters you must write ${10}. In practice, a script needing ten positional arguments should be taking options instead.

shift, and how it ends

$ ./sh.sh a b c d
start: $#=4 args=a b c d
after shift 2: $#=2 args=c d
shift 9 exit=1  $#=2

shift N drops the first N arguments and renumbers the rest. Shifting more than remain is a no-op that returns exit status 1 — usable as a loop condition, but do not assume it cleared the list.

getopts for short options

getopts is a shell builtin, so it needs no external command and behaves the same on every POSIX shell. The leading colon in ':vf:' turns on silent error reporting so you write your own messages:

while getopts ':vf:' opt; do
  case $opt in
    v)  verbose=1 ;;
    f)  file=$OPTARG ;;
    :)  echo "option -$OPTARG needs an argument" >&2; exit 2 ;;
    \?) echo "unknown option -$OPTARG" >&2; exit 2 ;;
  esac
done
shift $((OPTIND - 1))          # leave only the operands in "$@"
$ ./opts.sh -v -f "my report.txt" one "two words"
verbose=1 file=[my report.txt]
remaining $#=2
  operand [one]
  operand [two words]

$ ./opts.sh -vf out.log rest        # bundled short options
verbose=1 file=[out.log]
remaining $#=1
  operand [rest]

$ ./opts.sh -f          ; echo "exit=$?"     # missing argument
option -f needs an argument
exit=2

$ ./opts.sh --file=x    ; echo "exit=$?"     # long options are not supported
unknown option --
exit=2

shift $((OPTIND - 1)) is the line people forget; without it, the flags are still sitting in "$@" and get treated as filenames.

The getopt trap on macOS and BSD

One way to get long options is external getopt, with this guard:

PARSED=$(getopt -o f:v -l file:,verbose -- "$@")
if [ $? -ne 0 ]; then echo "Invalid options"; exit 1; fi

That approach is GNU-only, and the guard does not save you. On macOS 26.6 with the base-system /usr/bin/getopt:

$ /usr/bin/getopt -o f:v -l file:,verbose -- --file="my report.txt" -v extra
 -- f:v -l file:,verbose -- --file=my report.txt -v extra
$ echo $?
0

Exit status 0, garbage output, quoting destroyed — and then eval set -- "$PARSED" re-splits it into whatever that mess happens to word-split into. BSD getopt takes an optstring as its first operand and has no -l. If you need long options in a portable script, either parse them yourself in a while/case/shift loop, or require GNU getopt explicitly and check for it:

getopt --test >/dev/null 2>&1
[ $? -eq 4 ] || { echo "GNU getopt required" >&2; exit 1; }

GNU getopt on Linux: described, not run

GNU getopt was not installed on this machine, so its success path is described from its man page (“-T, --test: Test if your getopt(1) is this enhanced version or an old version. This generates no output, and sets the error status to 4″), not measured. The half of the guard that was executed here is the one that matters: BSD getopt --test returns 0, not 4, so the check correctly refuses to run. Everything in a $-prefixed block was run on macOS 26.6 with bash 5.3.15 on 2026-08-10.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.