Special Characters#

What makes a character special? If it has a meaning beyond its literal meaning, a meta-meaning, then we refer to it as a special character. Along with commands and keywords, special characters are building blocks of Bash scripts.

Special Shell Variables#

VariableMeaning
$0Filename 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 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

Operator Precedence#

In a script, operations execute in order of precedence: the higher precedence operations execute before the lower precedence ones.

OperatorMeaningComments
HIGHEST PRECEDENCE
var++ var–post-increment, post-decrementC-style operators
++var –varpre-increment, pre-decrement
! ~negationlogical / bitwise, inverts sense of following operator
**exponentiationarithmetic operation
* / %multiplication, division, moduloarithmetic operation
+ -addition, subtractionarithmetic operation
« »left, rightshift bitwise
-z -nunary comparisonstring is/is-not null
-e -f -t -x, etc.unary comparisonfile-test
< -lt > -gt <= -le >= -gecompound comparisonstring and integer
-nt -ot -efcompound comparisonfile-test
== -eq != -neequality / inequalitytest operators, string and integer
&ANDbitwise
^XORexclusive OR, bitwise
|ORbitwise
&& -aANDlogical, compound comparison
| -oORlogical, compound comparison
?:trinary operatorC-style
=assignment(do not confuse with equality test)
*= /= %= += -= «= »= &=combination assignmenttimes-equal, divide-equal,
,commalinks a sequence of operations
LOWEST PRECEDENCE

TEST Operators: Binary Comparison#

OperatorMeaningOperatorMeaning
Arithmetic ComparisonString 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