Unix Power ToolsUnix Power ToolsSearch this book

27.17. "Special" Characters and Operators

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:

As in other parts of this book, the sh entries apply to ksh and bash; the csh entries apply to tcsh.

Table 27-3. Special characters and their meanings

Character

Where

Meaning

Articles

ESC

csh

Filename completion.

Section 28.6

RETURN

csh, sh

Execute command.

 
space

csh, sh

Argument separator.

 
TAB

csh, sh

Argument separator.

 
TAB

bash

Completion (in interactive shells).

Section 28.6

#

csh, sh

Start a comment.

Section 35.1

`

csh, sh

Command substitution (backquotes).

Section 28.14

"

sh

Weak quotes.

Section 27.12

"

csh

Weak quotes.

Section 27.13

$var

csh, sh

Expand variable var.

Section 35.3, Section 35.9

${var}

csh, sh

Same as $var.

Section 35.9

$var:mod

csh

Edit var with modifier mod

Section 28.5

${var-default}

sh

If var not set, use default.

Section 36.7

${var:-default}

bash

If var not set or null, use default.

Section 36.7

${var=default}

sh

If var not set, set it to default and use that value.

Section 36.7

${var:=default}

bash

If var not set or null, set it to default and use that value.

Section 36.7

${var+instead}

sh

If var set, use instead. Otherwise, null string.

Section 36.7

${var:+instead}

bash

If var set or not null, use instead. Otherwise, null string.

Section 36.7

${var?message}

sh

If var set, use its value. Else print message (or default) and exit.

Section 36.7

${var:?message}

bash

If var set or not null, use its value. Else print message (or default) and exit.

Section 36.7

${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.

Section 35.9

${=spec}

zsh

Turn on sh_word_split option while evaluating spec.

Section 35.9

${~array}

zsh

Turn on glob_subst option while evaluating spec.

Section 35.9

|

csh, sh

Pipe standard output.

Section 1.5, Section 43.1

|&

csh

Pipe standard output and standard error.

Section 43.5

|&

ksh

Coroutine.

Section 24.4

^

sh only

Pipe character (obsolete).

 
^

csh, bash

Edit previous command line.

Section 30.5

&

csh, sh

Run program in background.

Section 23.2

&!

zsh

Run program in background without job control.

Section 23.11

&|

zsh

Run program in background without job control.

Section 23.11

?

csh, sh

Match one character.

Section 1.13, Section 33.2

*

csh, sh

Match zero or more characters.

Section 1.13, Section 33.2

;

csh, sh

Command separator.

 
;;

sh

End of case element.

Section 35.10

~

csh, ksh, bash

Home directory.

Section 31.11

~user

csh, ksh, bash

Home directory of user.

Section 31.11

!

csh, bash

Command history.

Section 30.2

!

bash, ksh93, zsh

Toggle exit status.

Section 35.12

-

zsh

Make a login shell.

Section 3.19

=

csh, sh

Assignment.

Section 35.9,

$#

csh, sh

Number of shell arguments or words in an array.

Section 35.20,

"$@"

sh

Original script arguments.

Section 35.20

$*

csh, sh

Original script arguments, broken into words.

Section 35.20

$?

sh

Status of previous command.

Section 35.12

$$

csh, sh

Process identification number.

Section 27.12

$!

sh

Process identification number of last background job.

Section 4.15

$<

csh

Read input from terminal.

Section 28.9

$_

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.

Section 35.14

cmd1 || cmd2

csh, sh

Execute cmd2 if cmd1 fails.

Section 35.14

$(..)

ksh, bash

Command substitution.

Section 36.24, Section 28.14

. file

sh

Execute commands from file in this shell.

Section 35.29

:

sh

Evaluate arguments, return true.

Section 35.6

:

sh

Separate values in paths.

Section 31.5, Section 35.6

:

csh

Variable modifier.

Section 28.5

[ ]

csh, sh

Match range of characters.

Section 1.13, Section 33.2

[ ]

sh

Test.

Section 35.26

%n

csh, ksh, bash, zsh

Job number n.

Section 23.3

(cmd;cmd)

csh, sh

Run cmd;cmd in a subshell.

Section 43.7

{ }

csh, bash

In-line expansions.

Section 28.4

>file

csh, sh

Redirect standard output.

Section 43.1

>! file

csh

Output to file, even if noclobber set and file exists.

Section 43.6

>| file

ksh, bash

Output to file, even if noclobber set and file exists.

Section 43.6

>>file

csh, sh

Append standard output.

Section 43.1

>>! file

csh

Append to file, even if noclobber set and file doesn't exist.

Section 43.6

<file

csh, sh

Redirect standard input.

Section 43.1

<<word

csh, sh

Read until word, do command and variable substitution.

Section 27.16, Section 28.13

<<\word

csh, sh

Read until word, no substitution.

Section 27.16

<<-word

sh

Read until word, ignoring leading TABs.

Section 27.16

<> file

ksh, bash, zsh

Open file for writing and reading.

 
>& file

csh, bash

Redirect standard output and standard error to file.

Section 43.5

m> file

sh

Redirect output file descriptor m to file.

Section 36.16

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.

Section 36.16

>& -

sh

Close standard output.

Section 36.16

m<& -

sh

Close input file descriptor m.

Section 36.16

n>&m

sh

Connect output file descriptor n to file descriptor m.

Section 36.16

m>& -

sh

Close output file descriptor m.

Section 36.16

-- BB and JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.