Appendix B. Reference Cards

The following reference cards provide a useful summary of certain scripting concepts. The foregoing text treats these matters in more depth, as well as giving usage examples.


Table B-1. Special Shell Variables

VariableMeaning
$0Name of script
$1Positional parameter #1
$2 - $9Positional parameters #2 - #9
${10}Positional parameter #10
$#Number of positional parameters
"$*"All the positional parameters (as a single word) *
"$@"All the positional parameters (as separate strings)
${#*}Number of command line parameters passed to script
${#@}Number of command line parameters passed to script
$?Return value
$$Process ID (PID) of script
$-Flags passed to script (using set)
$_Last argument of previous command
$!Process ID (PID) of last job run in background

* Must be quoted, otherwise it defaults to "$@".


Table B-2. TEST Operators: Binary Comparison

OperatorMeaning-----OperatorMeaning
     
Arithmetic Comparison  String Comparison 
-eqEqual to =Equal to
   ==Equal to
-neNot equal to !=Not equal to
-ltLess than \<Less than (ASCII) *
-leLess than or equal to   
-gtGreater than \>Greater than (ASCII) *
-geGreater than or equal to   
   -zString is empty
   -nString is not empty
     
Arithmetic Comparisonwithin double parentheses (( ... ))   
>Greater than   
>=Greater than or equal to   
<Less than   
<=Less than or equal to   

* If within a double-bracket [[ ... ]] test construct, then no escape \ is needed.


Table B-3. TEST Operators: Files

OperatorTests Whether-----OperatorTests Whether
-eFile exists -sFile is not zero size
-fFile is a regular file   
-dFile is a directory -rFile has read permission
-hFile is a symbolic link -wFile has write permission
-LFile is a symbolic link -xFile has execute permission
-bFile is a block device   
-cFile is a character device -gsgid flag set
-pFile is a pipe -usuid flag set
-SFile is a socket -k"sticky bit" set
-tFile is associated with a terminal   
     
-NFile modified since it was last read F1 -nt F2File F1 is newer than F2 *
-OYou own the file F1 -ot F2File F1 is older than F2 *
-GGroup id of file same as yours F1 -ef F2Files F1 and F2 are hard links to the same file *
     
!"NOT" (reverses sense of above tests)   

* Binary operator (requires two operands).


Table B-4. Parameter Substitution and Expansion

ExpressionMeaning
${var}Value of var, same as $var
  
${var-DEFAULT}If var not set, evaluate expression as $DEFAULT *
${var:-DEFAULT}If var not set or is empty, evaluate expression as $DEFAULT *
  
${var=DEFAULT}If var not set, evaluate expression as $DEFAULT *
${var:=DEFAULT}If var not set, evaluate expression as $DEFAULT *
  
${var+OTHER}If var set, evaluate expression as $OTHER, otherwise as null string
${var:+OTHER}If var set, evaluate expression as $OTHER, otherwise as null string
  
${var?ERR_MSG}If var not set, print $ERR_MSG *
${var:?ERR_MSG}If var not set, print $ERR_MSG *
  
${!varprefix*}Matches all previously declared variables beginning with varprefix
${!varprefix@}Matches all previously declared variables beginning with varprefix

* Of course if var is set, evaluate the expression as $var.


Table B-5. String Operations

ExpressionMeaning
${#string}Length of $string
  
${string:position}Extract substring from $string at $position
${string:position:length}Extract $length characters substring from $string at $position
  
${string#substring}Strip shortest match of $substring from front of $string
${string##substring}Strip longest match of $substring from front of $string
${string%substring}Strip shortest match of $substring from back of $string
${string%%substring}Strip longest match of $substring from back of $string
  
${string/substring/replacement}Replace first match of $substring with $replacement
${string//substring/replacement}Replace all matches of $substring with $replacement
${string/#substring/replacement}If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement}If $substring matches back end of $string, substitute $replacement for $substring
  
  
expr match "$string" '$substring'Length of matching $substring* at beginning of $string
expr "$string" : '$substring'Length of matching $substring* at beginning of $string
expr index "$string" $substringNumerical position in $string of first character in $substring that matches
expr substr $string $position $lengthExtract $length characters from $string starting at $position
expr match "$string" '\($substring\)'Extract $substring* at beginning of $string
expr "$string" : '\($substring\)'Extract $substring* at beginning of $string
expr match "$string" '.*\($substring\)'Extract $substring* at end of $string
expr "$string" : '.*\($substring\)'Extract $substring* at end of $string

* Where $substring is a regular expression.


Table B-6. Miscellaneous Constructs

ExpressionInterpretation
  
Brackets 
if [ CONDITION ]Test construct
if [[ CONDITION ]]Extended test construct
Array[1]=element1Array initialization
[a-z]Range of characters within a Regular Expression
  
Curly Brackets 
${variable}Parameter substitution
${!variable}Indirect variable reference
{ command1; command2; . . . commandN; }Block of code
{string1,string2,string3,...}Brace expansion
{a..z}Extended brace expansion
{}Text replacement, after find and xargs
  
  
Parentheses 
( command1; command2 )Command group executed within a subshell
Array=(element1 element2 element3)Array initialization
result=$(COMMAND)Execute command in subshell and assign result to variable
>(COMMAND)Process substitution
<(COMMAND)Process substitution
  
Double Parentheses 
(( var = 78 ))Integer arithmetic
var=$(( 20 + 5 ))Integer arithmetic, with variable assignment
  
Quoting 
"$variable""Weak" quoting
'string'"Strong" quoting
  
Back Quotes 
result=`COMMAND`Execute command in subshell and assign result to variable