The difference between $ # and $ {# @}

I looked at the following code and found that both $ # and $ {# @} print the same value. Can someone tell me what is the difference between the two?

# length.sh E_NO_ARGS=65 if [ $# -eq 0 ] # Must have command-line args to demo script. then echo "Please invoke this script with one or more command-line arguments." exit $E_NO_ARGS fi var01=abcdEFGH28ij echo "var01 = ${var01}" echo "Length of var01 = ${#var01}" # Now, let try embedding a space. var02="abcd EFGH28ij" echo "var02 = ${var02}" echo "Length of var02 = ${#var02}" echo "Number of command-line arguments passed to script = ${#@}" echo "Number of command-line arguments passed to script = $#" exit 0 
+5
source share
1 answer

On the manual page ( 3.5.3 Enhancing Shell Settings ):

${#parameter}

The length of the parameter. The length in characters of the parameter value is replaced. If the parameter is * or @ , the value being replaced is the number of positional parameters. If the parameter is the name of the array, trimmed with the * or @ character, the value being replaced is the number of elements in the array.

So ${#@} is a special case, because $# is the usual way of expressing the number of positional parameters.

+8
source

All Articles