Positional Parameters

When a program is executed with a list of options and arguments,
  these are passed in as positional parameters.

Command examines and determines validity of option/argument.

  • They can be referenced with the $ followed by the index in the list.
  • $0 - indicates the program itself.
  • Additional parameters are numbered sequentially.
  • $0-$9 can be directly referenced.
  • Use {} to reference values > 9. bash echo ${11}
  • Maximum parameter limit, count = 0x7FFFFFFF or 2Gig. (2^31) length = page-size * 32 (page = 4k?) ~ 128K /usr/include/linux/binfmts.h
  • shift - shifts arguments left (down by 1). Discards previous $1. Old way of handling 10 or more arguments.
  • Quoted strings treated as single argument if referenced right. Use quotes around argument inside script i.e "$1" Accessing whole argument list except $0
  • $* - lists all arguments. Quotes on arguments stripped - list treated as individual words.
  • "$*" - lists all arguments. Quotes on arguments stripped - then whole list treated as single long string.
  • $@ - lists all arguments. Quotes on arguments stripped - list treated as individual words.
  • "$@" - lists all arguments. Quotes on arguments not stripped - quotes honored.
    #!/bin/bash
    
    #save this in a file such as arglister.sh
    #  and make it executable, then as follows :  
    #  ./arglister.sh one "and two" and a three
    
    echo using '$*'
    for arg in $*
    do 
      echo $arg
    done
    
    echo ""
    echo using '"$*"'
    for arg in "$*"
    do 
      echo $arg
    done
    
    echo ""
    echo using '$@'
    for arg in $@
    do 
      echo $arg
    done
    
    echo ""
    echo using '"$@"'
    for arg in "$@"
    do 
      echo $arg
    done
    
    Positional parameters are useful when writing shell scripts and functions.