7

I have a script with this:

login {
    # checking parameters -> if not ok print error and exit script
    if [ $# -lt 2 ] || [ $1 == '' ] || [ $2 == '' ]; then
        echo "Please check the needed options (username and password)"
        echo ""
        echo "For further Information see Section 13"
        echo ""
        echo "Press any key to exit"
        read
        exit
    fi

  } # /login

But I really dont know what the $# means on the third line.

dominique120
  • 1,170
  • 4
  • 18
  • 31

2 Answers2

10

The pound sign counts things.

  1. If it's just $#, it's the number of positional parameters, like $1, $2, $3. (Not counting $0, mind you.)
  2. If it's ${#var}, it's the number of characters in the expansion of the parameter. (String length)
  3. If it's ${#var[@]}, it's the number of elements in the array. Since bash arrays are sparse, this can be different from the index of the last element plus one.
kojiro
  • 74,557
  • 19
  • 143
  • 201
2

It's the number of arguments passed.

You can read it here, search for "Detecting command line arguments"

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78