sajad torkamani

In Bash, $# gives you the number of positional parameters (i.e., how many arguments were passed to a script or function).

Inside scripts

For example, if you run:

./script.sh one two "three four"

Inside script.sh, $# will be 3 because you passed the script three arguments.

Inside functions

Within a function, $# gives the number of arguments passed to the functional call. For example:

print_count() {
  echo "Function args: $#"
}

print_count a b c   # prints 3

Check number of arguments

if [[ $# -lt 2 ]]; then
  echo "You must provide 2 arguments"
fi
Tagged: Bash