$# 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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
`$` refer to `value of` and `#` refer to `number of / total number`
`$#` 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`
the number of parameters with which the script has been called
the number of parameters which have been set within the script by
set -- foo bar
(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".