cmd:bash positional parameter 

*Tags*: cmd:set

Usage 

set positional parameters 

$ set a1 'b2 more' "c3 333" d4

use positional parameters 

 $ echo $1
 a1

 $ echo $2
 b2 more

 $ echo $3
 c3 333

 $ for pp in "$@"; do echo $pp; done
 a1
 b2 more
 c3 333
 d4

 $ for pp in "$*"; do echo $pp; done
 a1 b2 more c3 333 d4

 $ for pp in $*; do echo $pp; done
 a1
 b2
 more
 c3
 333
 d4

 set -- "$@"

 $ echo $2
 b2 more

clear positional parameters 

 $ set --

 $ echo $1

substitute with new set 

 set -- $files
 set -- `getopt "hp:s:" "$@"` || fhelp
 set x `date`

from command, with space 

 $ ls -d S*
 SANS Intrusion Dectection.zip
 SONET - SDH Comp.rar
 [...]

 lst=`ls -d S*`
 set -- $lst

 $ echo $2
 Intrusion

 lst=`ls -Qd S*`
 eval set -- $lst

 $ echo $2
 SONET - SDH Comp.rar

 set -- $lst

 $ echo $1,$2
 "SANS,Intrusion

 set a1 'b2 more' "c3 333" d4
 eval set -- "$@" $lst

 $ echo $2
 b2

Help 

Positional Parameters
    A  positional parameter is a parameter denoted by one or more digits,
    other than the single digit 0.  Positional  parameters  are  assigned
    from  the shell's arguments when it is invoked, and may be reassigned
    using the set builtin command.

Comments 

   set x `date`   # Get the current day of the month into $4
   set -- `getopt "hp:s:" "$@"` || fhelp

Q: Why to clear positional parameter before setting it?

A: — is for getopt, not set. 'set a1' then 'echo $2' will yield nothing.

Q: Why there sticked in an 'x' between set and `date`?

A: in case the date or getopt system calls fail, we won't get wrong returns from an empty set statement. Also, in case some of the positional parameters begin with a -.