cited from here
1. Variables
${ |
The value of variable var . |
${ |
Select word or words in position i of var . i can be a single number, a range |
${# |
The number of words in var . |
${#argv} |
The number of command-line arguments. |
${argv[ |
Individual arguments on command line (positional parameters). n is a number (1, 12, etc.). |
${ |
Same as |
${argv[*]} |
All arguments on command line. |
$* |
Same as |
${argv[$#argv]} |
The last argument. |
${? |
Return 1 if var is set; 0 if var is not set. |
! ${? |
Return 0 if var is set; 1 if var is not set. |
$$ |
Process number of current shell; useful as part of a filename for creating temporary files with unique names. |
$< |
Read a line from standard input. |
2. Expressions
Group all other expressions inside ( ). Parentheses are required if the expression contains <
, >
, &
, or |
.
3. Operators
3.1 Assignment Operators
= |
Assign value. |
+= -= |
Reassign after addition/subtraction. |
*= /= %= |
Reassign after multiplication/division/remainder. |
&= ^= |= |
Reassign after bitwise AND/XOR/OR. |
++ |
Increment. |
– |
Decrement. |
3.2 Arithmetic Operators
* / % |
Multiplication; integer division; modulus (remainder). |
c+ – |
Addition; subtraction. |
3.3 Bitwise and Logical Operators
~ |
Binary inversion (one’s complement). |
! |
Logical negation. |
<< >> |
Bitwise left shift; bitwise right shift. |
& |
Bitwise AND. |
^ |
Bitwise exclusive OR. |
| |
Bitwise OR. |
&& |
Logical AND. |
|| |
Logical OR. |
{ |
Return 1 if command cmd is successful; 0 otherwise. Note that this is the opposite of cmd ‘s normal return code. The status variable may be more practical. |
3.4 Comparison Operators
== != |
Equality; inequality. |
<= >= |
Less than or equal to; greater than or equal to. |
< > |
Less than; greater than. |
=~ |
String on left matches a filename pattern on the right containing |
!~ |
String on left does not match a filename pattern containing |
3.5 File Inquiry Operators
Command substitution and filename expansion are performed on file before the test is performed.
-d |
The file is a directory. |
-e |
The file exists. |
-f |
The file is a plain file. |
-o |
The user owns the file. |
-r |
The user has read permission. |
-w |
The user has write permission. |
-x |
The user has execute permission. |
-z |
The file has zero size. |
! |
Reverse the sense of any of the above inquiries. |
Examples.
The following examples show @
commands and assume n = 4:
Expression | Value of $x |
---|---|
@ x = ($n > 10 || $n < 5) | 1 |
@ x = ($n >= 0 && $n < 3) | 0 |
@ x = ($n << 2) | 16 |
@ x = ($n >> 2) | 1 |
@ x = $n % 2 | 0 |
@ x = $n % 3 | 1 |
The following examples show the first line of if or while statements:
Expression |
Meaning |
---|---|
while ($#argv != 0) |
While there are command-line ( argv ) arguments … |
if ($today[1] == Fri) |
If the first word is Fri … |
if ($file !~ *.[zZ]) |
If the file doesn’t end with .z or .Z … |
if ($argv[1] =~ chap?) |
If the first argument is chap followed by a single character… |
if (-f $argv[1]) |
If the first argument is a plain file… |
if (! -d $tmpdir) |
If tmpdir is not a directory… |