You are on page 1of 4

Table B-1. Special Shell Variables Variable Meaning $0 $1 ${10} $# "$*" "$@" ${#*} ${#@} $? $$ $$_ $!

Filename of script Positional parameter #1 Positional parameter #10 Number of positional parameters All the positional parameters (as a single word) * All the positional parameters (as separate strings) Number of positional parameters Number of positional parameters 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

$2 - $9 Positional parameters #2 - #9

* Must be quoted, otherwise it defaults to "$@". Table B-2. TEST Operators: Binary Comparison Operator Arithmetic Comparison -eq -ne -lt -le -gt -ge Equal to Not equal to Less than Less than or equal to Greater than Greater than or equal to -z -n Arithmetic Comparison within double parentheses (( ... )) > >= < <= Greater than Greater than or equal to Less than Less than or equal to String is empty String is not empty \> Greater than (ASCII) * Meaning ----- Operator String Comparison = == != \< Equal to Equal to Not equal to Less than (ASCII) * Meaning

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

Table B-3. TEST Operators: Files Operator Tests Whether -e -f -d -h -L -b -c -p -S -t -N -O -G ! File exists File is a regular file File is a directory File is a symbolic link File is a symbolic link File is a block device File is a character device File is a pipe File is a socket File is associated with a terminal File modified since it was last read You own the file Group id of file same as yours NOT (inverts sense of above tests) F1 -nt F2 File F1 is newer than F2 * F1 -ot F2 File F1 is older than F2 * F1 -ef F2 Files F1 and F2 are hard links to the same file * -g -u -k sgid flag set suid flag set "sticky bit" set -r -w -x File has read permission File has write permission File has execute permission ----- Operator -s Tests Whether File is not zero size

* Binary operator (requires two operands). Table B-4. Parameter Substitution and Expansion Expression ${var} ${var-DEFAULT} Meaning Value of var (same as $var) 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} ${var:+OTHER} ${var?ERR_MSG} If var set, evaluate expression as $OTHER, otherwise as null string If var set, evaluate expression as $OTHER, otherwise as null string If var not set, print $ERR_MSG and abort script with an exit status of 1.*

${var:?ERR_MSG} If var not set, print $ERR_MSG and abort script with an exit status of 1.* ${!varprefix*} ${!varprefix@} Matches all previously declared variables beginning with varprefix Matches all previously declared variables beginning with varprefix

* If var is set, evaluate the expression as $var with no side-effects.

Table B-5. String Operations Expression ${#string} ${string:position} ${string:position:length} Meaning Length of $string Extract substring from $string at $position Extract $length characters substring from $string at $position [zero-indexed, first character is at position 0] Strip shortest match of $substring from front of $string Strip longest match of $substring from front of $string Strip shortest match of $substring from back of $string Strip longest match of $substring from back of $string Replace first match of $substring with $replacement Replace all matches of $substring with $replacement If $substring matches front end of $string, substitute $replacement for $substring If $substring matches back end of $string, substitute $replacement for $substring

${string#substring} ${string##substring} ${string%substring} ${string%%substring} ${string/substring/replacement} ${string//substring/replacement} ${string/#substring/replacement} ${string/%substring/replacement}

expr match "$string" '$substring' expr "$string" : '$substring' expr index "$string" $substring expr substr $string $position $length expr match "$string" '\($substring\)' expr "$string" : '\($substring\)' expr match "$string" '.*\($substring\)'

Length of matching $substring* at beginning of $string Length of matching $substring* at beginning of $string Numerical position in $string of first character in $substring* that matches [0 if no match, first character counts as position 1] Extract $length characters from $string starting at $position [0 if no match, first character counts as position 1] Extract $substring*, searching from beginning of $string Extract $substring* , searching from beginning of $string Extract $substring*, searching from end of $string

expr "$string" : '.*\($substring\)' Extract $substring*, searching from end of $string * Where $substring is a Regular Expression. Table B-6. Miscellaneous Constructs Expression Brackets if [ CONDITION ] if [[ CONDITION ]] Array[1]=element1 Test construct Extended test construct Array initialization Interpretation

Expression [a-z] Curly Brackets ${variable} ${!variable} {string1,string2,string3,...} {a..z} {}

Interpretation Range of characters within a Regular Expression

Parameter substitution Indirect variable reference Brace expansion Extended brace expansion Text replacement, after find and xargs

{ command1; command2; . . . commandN; } Block of code

Parentheses ( command1; command2 ) Array=(element1 element2 element3) result=$(COMMAND) >(COMMAND) <(COMMAND) Double Parentheses (( var = 78 )) var=$(( 20 + 5 )) (( var++ )) (( var-- )) (( var0 = var1<98?9:21 )) Quoting "$variable" 'string' Back Quotes result=`COMMAND` Command substitution, classic style "Weak" quoting 'Strong' quoting Integer arithmetic Integer arithmetic, with variable assignment C-style variable increment C-style variable decrement C-style trinary operation Command group executed within a subshell Array initialization Command substitution, new style Process substitution Process substitution

You might also like