Shell Scripting – What Does $# Mean?

shellshell-script

What does $# mean in shell?

I have code such as

if [ $# -eq 0 ]
then

I want to understand what $# means, but Google search is very bad for searching these kinds of things.

Best Answer

You can always check the man page of your shell. man bash says:

Special Parameters
   #      Expands to the number of positional parameters in decimal.

Therefore a shell script can check how many parameters are given with code like this:

if [ "$#" -eq 0 ]; then
  echo "you did not pass any parameter"
fi