home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeBook TitleSearch this book

5.2. Syntax

This section describes the many symbols peculiar to the C shell. The topics are arranged as follows:

  • Special files

  • Filename metacharacters

  • Quoting

  • Command forms

  • Redirection forms

5.2.3. Quoting

Quoting disables a character's special meaning and allows it to be used literally, as itself. The characters in the following table have special meaning to the C shell.

CharacterMeaning
;Command separator
&Background execution
( )Command grouping
|Pipe
* ? [ ] ~Filename metacharacters
{ }

String expansion characters; usually don't require quoting

< > & !Redirection symbols
! ^History substitution, quick substitution
" ' \Used in quoting other characters
Command substitution
$Variable substitution
space tab newlineWord separators

These characters can be used for quoting:

" "
Everything between " and " is taken literally, except for the following characters that keep their special meaning:

$
Variable substitution will occur.

Command substitution will occur.

"
This marks the end of the double quote.

\
Escape next character.

!
The history character.

newline
The newline character.

' '
Everything between ' and ' is taken literally except for ! (history) and another ', and newline.

\
The character following a \ is taken literally. Use within "" to escape ", $, , and newline. Use within '' to escape newlines. Often used to escape itself, spaces, or newlines. Always needed to escape a history character (usually !).

5.2.3.1. Examples

% echo 'Single quotes "protect" double quotes'
Single quotes "protect" double quotes

% echo "Don't double quotes protect single quotes too?"
Don't double quotes protect single quotes too?

% echo "You have `ls|wc -l` files in `pwd`"
You have       43 files in /home/bob

% echo The value of \$x is $x
The value of $x is 100

5.2.5. Redirection Forms

File DesciptorNameCommon AbbreviationTypical Default
0Standard inputstdinKeyboard
1Standard outputstdoutTerminal
2Standard errorstderrTerminal

The usual input source or output destination can be changed, as seen in the following sections.

5.2.5.1. Simple redirection

cmd > file
Send output of cmd to file (overwrite).

cmd >! file
Same as above, even if noclobber is set.

cmd >> file
Send output of cmd to file (append).

cmd >>! file
Same as above, but write to file even if noclobber is set.

cmd < file
Take input for cmd from file.

cmd << text
Read standard input up to a line identical to text (text can be stored in a shell variable). Input is usually typed at the terminal or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is quoted (using any of the shell-quoting mechanisms), the input is passed through verbatim.

5.2.5.2. Multiple redirection

cmd >& fileSend both standard output and standard error to file.
cmd >&! fileSame as above, even if noclobber is set.
cmd >>& file

Append standard output and standard error to end of file.

cmd >>&! file

Same as above, but append to or create file even if noclobber is set.

cmd1 |& cmd2Pipe standard error together with standard output.
(cmd > f1) >& f2

Send standard output to file f1, standard error to file f2.

cmd | tee files

Send output of cmd to standard output (usually the terminal) and to files. (See the Example in Chapter 2, under tee.)

5.2.5.3. Examples

% cat part1 > book
% cat part2 part3 >> book
% mail tim < report
% cc calc.c >& error_out
% cc newcalc.c >&! error_out
% grep Unix ch* |& pr
% (find / -print > filelist) >& no_access

% sed 's/^/XX /g' << "END_ARCHIVE"
This is often how a shell archive is "wrapped",
bundling text for distribution.  You would normally
run sed from a shell program, not from the command line.


"END_ARCHIVE"
XX This is often how a shell archive is "wrapped",
XX bundling text for distribution.  You would normally
XX run sed from a shell program, not from the command line.


Library Navigation Links

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