Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What does $# mean in shell?

I have code such as

if [ $# -eq 0 ]

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

share|improve this question
For searches with punctuation marks, I like to use symbolhound.com. Usually finds what I need. – Eric Andres Mar 31 '14 at 21:58
you can browse the manual – miracle173 Apr 4 '14 at 12:04
For better referencing and SEO: bang, number sign, sharp, U+0023, #, comment sign and dollar! – Clément Jul 16 '15 at 13:28
up vote 21 down vote accepted

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"
share|improve this answer
thanks man. Didnt know I can man bash – BufBills Mar 31 '14 at 5:40
@NoName You can man almost anything, including man itself. Also try apropos some time. – Michael Kjörling Mar 31 '14 at 9:08
@NoName you can also, when using an OS with GNU tools (linux, etc) use info for either the man page, or additionnal informations (very detailled, and compartmentalized) if the packages has some – Olivier Dulac Mar 31 '14 at 12:29

That is

  1. the number of parameters with which the script has been called

  2. the number of parameters which have been set within the script by set -- foo bar

  3. (when used within a function) the number of parameters with which a function has been called (set would work there, too).

This is explained in the bash man page in the block "Special Parameters".

share|improve this answer


`$` refer to `value of` and
`#` refer to `number of / total number`

So together

`$#` refer to `The value of the total number of command line arguments passed.`

Thus, you can use $# to check the number of arguments/parameters passed like you did and handle any unexpected situations.

Similarly, we have

`$1` for `value of 1st argument passed`
`$2` for 'value of 2nd argument passed`


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.