Before you learn about regular expressions (Section 32.1), you should understand how quoting (Section 27.12) works in Unix.
Regular expressions use metacharacters. The shells also have metacharacters. Metacharacters are simply characters that have a special meaning. The problem occurs when you want to use a regular expression in a shell script. Will the shell do something special with the character? Or will it be passed unchanged to the program? The $ character is a good example of a regular expression metacharacter that is also used by the shell, but whose meaning is different depending upon who interprets it, the shell or other programs. It could be the beginning of a variable name or it could be part of a regular expression (Section 32.2). If you need a regular expression, you must know whether any of the characters of the expression are metacharacters, and must know the right way to quote that character so that it is passed to the program without being modified by the shell.
Table 27-3 is a table of special characters and operators in the shells covered by this book. (Because zsh acts basically like both C-type and Bourne-type shells, its name would show up in every entry. So we don't list it here unless an entry applies only to zsh -- or one or two other shells.) The chart also includes several combinations of characters just to be complete. But, to keep things simple, it doesn't include:
Arithmetic operators like +, -, and so on; see the articles on built-in arithmetic for a list.
History substitution like !!, !$, and so on; see Section 30.8 instead.
As in other parts of this book, the sh entries apply to ksh and bash; the csh entries apply to tcsh.
Character |
Where |
Meaning |
Articles |
---|---|---|---|
ESC |
csh |
Filename completion. |
|
RETURN |
csh, sh |
Execute command. |
|
space |
csh, sh |
Argument separator. |
|
TAB |
csh, sh |
Argument separator. |
|
TAB |
bash |
Completion (in interactive shells). |
|
# |
csh, sh |
Start a comment. |
|
` |
csh, sh |
Command substitution (backquotes). |
|
" |
sh |
Weak quotes. |
|
" |
csh |
Weak quotes. |
|
$var |
csh, sh |
Expand variable var. |
|
${var} |
csh, sh |
Same as $var. |
|
$var:mod |
csh |
Edit var with modifier mod |
|
${var-default} |
sh |
If var not set, use default. |
|
${var:-default} |
bash |
If var not set or null, use default. |
|
${var=default} |
sh |
If var not set, set it to default and use that value. |
|
${var:=default} |
bash |
If var not set or null, set it to default and use that value. |
|
${var+instead} |
sh |
If var set, use instead. Otherwise, null string. |
|
${var:+instead} |
bash |
If var set or not null, use instead. Otherwise, null string. |
|
${var?message} |
sh |
If var set, use its value. Else print message (or default) and exit. |
|
${var:?message} |
bash |
If var set or not null, use its value. Else print message (or default) and exit. |
|
${var#pat} |
ksh, bash |
Value of var with smallest pat deleted from start. |
|
${var##pat} |
ksh, bash |
Value of var with largest pat deleted from start. |
|
${var%pat} |
ksh, bash |
Value of var with smallest pat deleted from end. |
|
${var%%pat} |
ksh, bash |
Value of var with largest pat deleted from end. |
|
${^array} |
zsh |
Expand array in place, like rc_expand_param option. |
|
${=spec} |
zsh |
Turn on sh_word_split option while evaluating spec. |
|
${~array} |
zsh |
Turn on glob_subst option while evaluating spec. |
|
| |
csh, sh |
Pipe standard output. |
|
|& |
csh |
Pipe standard output and standard error. |
|
|& |
ksh |
Coroutine. |
|
^ |
sh only |
Pipe character (obsolete). |
|
^ |
csh, bash |
Edit previous command line. |
|
& |
csh, sh |
Run program in background. |
|
&! |
zsh |
Run program in background without job control. |
|
&| |
zsh |
Run program in background without job control. |
|
? |
csh, sh |
Match one character. |
|
* |
csh, sh |
Match zero or more characters. |
|
; |
csh, sh |
Command separator. |
|
;; |
sh |
End of case element. |
|
~ |
csh, ksh, bash |
Home directory. |
|
~user |
csh, ksh, bash |
Home directory of user. |
|
! |
csh, bash |
Command history. |
|
! |
bash, ksh93, zsh |
Toggle exit status. |
|
- |
zsh |
Make a login shell. |
|
= |
csh, sh |
Assignment. |
|
$# |
csh, sh |
Number of shell arguments or words in an array. |
|
"$@" |
sh |
Original script arguments. |
|
$* |
csh, sh |
Original script arguments, broken into words. |
|
$? |
sh |
Status of previous command. |
|
$$ |
csh, sh |
Process identification number. |
|
$! |
sh |
Process identification number of last background job. |
|
$< |
csh |
Read input from terminal. |
|
$_ |
bash, ksh, zsh |
Last argument of previous command. |
|
$n |
sh |
Argument n. 1 <= n <= 9 for most shells; bash and ksh93 support ${n} for n >= 10. |
|
$0 |
sh |
Name of the shell or shell script. |
|
cmd1 && cmd2 |
csh, sh |
Execute cmd2 if cmd1 succeeds. |
|
cmd1 || cmd2 |
csh, sh |
Execute cmd2 if cmd1 fails. |
|
$(..) |
ksh, bash |
Command substitution. |
|
. file |
sh |
Execute commands from file in this shell. |
|
: |
sh |
Evaluate arguments, return true. |
|
: |
sh |
Separate values in paths. |
|
: |
csh |
Variable modifier. |
|
[ ] |
csh, sh |
Match range of characters. |
|
[ ] |
sh |
Test. |
|
%n |
csh, ksh, bash, zsh |
Job number n. |
|
(cmd;cmd) |
csh, sh |
Run cmd;cmd in a subshell. |
|
{ } |
csh, bash |
In-line expansions. |
|
>file |
csh, sh |
Redirect standard output. |
|
>! file |
csh |
Output to file, even if noclobber set and file exists. |
|
>| file |
ksh, bash |
Output to file, even if noclobber set and file exists. |
|
>>file |
csh, sh |
Append standard output. |
|
>>! file |
csh |
Append to file, even if noclobber set and file doesn't exist. |
|
<file |
csh, sh |
Redirect standard input. |
|
<<word |
csh, sh |
Read until word, do command and variable substitution. |
|
<<\word |
csh, sh |
Read until word, no substitution. |
|
<<-word |
sh |
Read until word, ignoring leading TABs. |
|
<> file |
ksh, bash, zsh |
Open file for writing and reading. |
|
>& file |
csh, bash |
Redirect standard output and standard error to file. |
|
m> file |
sh |
Redirect output file descriptor m to file. |
|
m>> file |
sh |
Append output file descriptor m to file. |
|
m< file |
sh |
Redirect input file descriptor m from file. |
|
<&m |
sh |
Take standard input from file descriptor m. |
|
>&m |
sh |
Use file descriptor m as standard output. |
|
>& - |
sh |
Close standard output. |
|
m<& - |
sh |
Close input file descriptor m. |
|
n>&m |
sh |
Connect output file descriptor n to file descriptor m. |
|
m>& - |
sh |
Close output file descriptor m. |
Copyright © 2003 O'Reilly & Associates. All rights reserved.