$# $n

$# - number of arguments
$n - nth argument.
$0 contains the full pathname of the first item on the command line (i.e., the name of the program).
To specify a number greater than nine, surround the number in braces, as in ${10}, ${55}, ${211}, and so on.

  1. #!/bin/bash
  2. # posit-param: script to view command line parameters
  3. echo "
  4. Number of arguments: $#
  5. \$0 = $0
  6. \$1 = $1
  7. \$2 = $2
  8. \$3 = $3
  9. \$4 = $4
  10. \$5 = $5
  11. \$6 = $6
  12. \$7 = $7
  13. \$8 = $8
  14. \$9 = $9
  15. \$10 = ${10}
  16. "

shift

Each time shift is executed, the value of $2 is moved to $1, the value of $3 is moved to $2, and so on. The value of $# is also reduced by one.

  1. #!/bin/bash
  2. # posit-param2: script to display all arguments
  3. count=1
  4. while [[ $# -gt 0 ]]; do
  5. echo "Argument $count = $1"
  6. count=$((count + 1))
  7. shift
  8. done

Examples

  1. #!/bin/bash
  2. # file-info: simple file information program
  3. PROGNAME="$(basename "$0")"
  4. if [[ -e "$1" ]]; then
  5. echo -e "\nFile Type:"
  6. file "$1"
  7. echo -e "\nFile Status:"
  8. stat "$1"
  9. else
  10. echo "$PROGNAME: usage: $PROGNAME file" >&2
  11. exit 1
  12. fi

Using Positional Parameters with Shell Functions

  1. !/bin/bash
  2. #************************************************
  3. # Author: Ronnie Ming
  4. # Filename: posit_param.sh
  5. # Date: 2021-05-04 16:11:19
  6. # Description: ...
  7. #************************************************
  8. function file_info() {
  9. if [[ -e "$1" ]]; then
  10. echo -e "\nFile Type:"
  11. file "$1"
  12. echo -e "\nFile Status:"
  13. stat "$1"
  14. else
  15. echo "$FUNCNAME: usage: $FUNCNAME file" >2&
  16. return 1
  17. fi
  18. }
  19. file_info posit_param.sh

Note that $0 always contains the full pathname of the first item on the command line (i.e., the name of the program) and does not contain the name of the shell function.

Handling Positional Parameters en Masse

Parameter Description
$* Expands into the list of positional parameters, starting with 1. When surrounded by double quotes, it expands into a double-quoted string containing all of the positional parameters, each separated by the first character of the IFS shell variable (by default a space character).
$@ Expands into the list of positional parameters, starting with 1. When surrounded by double quotes, it expands each positional parameter into a separate word as if it was surrounded by double quotes.
  1. #!/bin/bash
  2. # posit-params3: script to demonstrate $* and $@
  3. print_params() {
  4. echo "\$1 = $1"
  5. echo "\$2 = $2"
  6. echo "\$3 = $3"
  7. echo "\$4 = $4"
  8. }
  9. pass_params() {
  10. echo -e "\n" '$* :'; print_params $*
  11. echo -e "\n" '"$*" :'; print_params "$*"
  12. echo -e "\n" '$@ :'; print_params $@
  13. echo -e "\n" '"$@" :'; print_params "$@"
  14. }
  15. pass_params "word" "words with spaces"
  1. $ ./posit-param3
  2. $* :
  3. $1 = word
  4. $2 = words
  5. $3 = with
  6. $4 = spaces
  7. "$*" :
  8. $1 = word words with spaces
  9. $2 =
  10. $3 =
  11. $4 =
  12. $@ :
  13. $1 = word
  14. $2 = words
  15. $3 = with
  16. $4 = spaces
  17. "$@" :
  18. $1 = word
  19. $2 = words with spaces
  20. $3 =
  21. $4 =

"$@" is by far the most useful for most situations because it preserves the integrity of each positional parameter.