Classic Shell Scripting - Arnold Robbins [211]
some command $( ...
case $var in
( foo | bar ) some other command ;;
( stuff | junk ) something else again ;;
esac
... )
ksh93, bash, and POSIX allow an optional open parenthesis on case selectors, but do not require it. (Thus, ksh93 is smarter than ksh88, which required the open parenthesis inside $(...).)
Printing traps with trap -p
According to POSIX, an unadorned trap command prints out the state of the shell's traps, in a form that can be reread by the shell later to restore the same traps. Both shells also allow you to use trap -p to print out the traps.
Here strings with <<<
It's common to use echo to generate a single line of input for further processing. For example:
echo $myvar1 $mvar2 | tr ... | ...
Both shells support a notation we term here strings, taken from the Unix version of the rc shell.[3] Here strings use <<< followed by a string. The string becomes the standard input to the associated command, with the shell automatically supplying a final newline:
tr ... <<< "$myvar1 $myvar2" | ...
This potentially saves the creation of an extra process and is also notationally clear.
Extended string notation
Both bash and ksh93 support a special string notation that understands the usual set of C-like (or echo-like) escape sequences. The notation consists of a $ in front of a single-quoted string. Such strings behave like regular single-quoted strings, but the shell interprets escape sequences inside the string. For example:
$ echo $'A\tB'
A, tab, B
A B
$ echo $'A\nB'
A, newline, B
A
B
Table 14-5 lists the arithmetic operators supported by both bash and ksh93.
Table 14-5. bash and ksh93 arithmetic operators
Operator
Meaning
Associativity
++ --
Increment and decrement, prefix and postfix
Left to right
+ - ! ~
Unary plus and minus; logical and bitwise negation
Right to left
**
Exponentiation[4]
Right to left
* / %
Multiplication, division, and remainder
Left to right
+ -
Addition and subtraction
Left to right
<< >>
Bit-shift left and right
Left to right
< <= > >=
Comparisons
Left to right
= = !=
Equal and not equal
Left to right
&
Bitwise AND
Left to right
^
Bitwise Exclusive OR
Left to right
|
Bitwise OR
Left to right
&&
Logical AND (short-circuit)
Left to right
||
Logical OR (short-circuit)
Left to right
?:
Conditional expression
Right to left
= += -= *= /= %= &= ^= <<= >>= |=
As sign ment operators
Right to left
,
Sequential evaluation
Left to right
[4] ksh93m and newer. In bash versions prior to 3.1, ** is left-associative. It will be right-associative starting with version 3.1 The ** operator is not in the C language.
Parentheses can be used to group subexpressions. The arithmetic expression syntax (like C) supports relational operators as "truth values" of 1 for true and 0 for false.
For example, $((3 > 2)) has the value 1; $(( (3 > 2) || (4 <= 1) )) also has the value 1, since at least one of the two subexpressions is true.
* * *
[2] And, for that matter, the same as in grep, sed, ed, vi, etc. One notable difference is that the shell uses ! inside [...] for negation, whereas the various utilities all use ^.
[3] See http://www.star.le.ac.uk/~tjg/rc/.
Download Information
This section briefly describes where to find source code for bash and ksh93, and how to build each shell from source code. It assumes that you have a C compiler and the make program available on your system.
bash
bash is available from the Free Software Foundation GNU Project's FTP server. As of this writing, the current version is 3.0. You can use wget (if you have it) to retrieve the distribution tar file:
$ wget ftp://ftp.gnu.org/gnu/bash/bash-3.0.tar.gz
--17:49:21-- ftp://ftp.gnu.org/gnu/bash/bash-3.0.tar.gz
=> `bash-3.0.tar.gz'
...
Alternatively, you can use good old-fashioned anonymous FTP to