[Bash] Count command-line arguments

Created on November 30, 2026

Use $# to get the number of arguments passed to a shell script. Save this example as dummy.sh.

#!/usr/bin/env bash

echo "$#"

Pass two arguments, hello and world.

bash dummy.sh hello world

Output

2

You can use the count in an if statement, for example, to print a message when the caller provides fewer than two arguments.

if [ "$#" -lt 2 ]; then
  echo "Specify at least two arguments"
fi
Bash