Introduction to Unix

../_images/ksuPoly5.jpg ../_images/UNIX_blocks5.jpeg

4.3. Shell Metacharacters

Linux for Programmers and Users, Section 5.5.

As was discussed in Structure of a Command, the command options, option arguments and command arguments are separated by the space character. However, we can also use special characters called metacharacters in a Unix command that the shell interprets rather than passing to the command.

The Shell Metacharacters are listed here for reference. Many of the metacharacters are described elsewhere in the study guide.

Symbol Meaning
> Output redirection, (see File Redirection)
>> Output redirection (append)
< Input redirection
* File substitution wildcard; zero or more characters
? File substitution wildcard; one character
[ ] File substitution wildcard; any character between brackets
`cmd` Command Substitution
$(cmd) Command Substitution
| The Pipe (|)
; Command sequence, Sequences of Commands
|| OR conditional execution
&& AND conditional execution
( ) Group commands, Sequences of Commands
& Run command in the background, Background Processes
# Comment
$ Expand the value of a variable
\ Prevent or escape interpretation of the next character
<< Input redirection (see Here Documents)

4.3.1. How to Avoid Shell Interpretation

Linux for Programmers and Users, Section 5.16.

Sometimes we need to pass metacharacters to the command being run and do not want the shell to interpret them. There are three options to avoid shell interpretation of metacharacters.

  1. Escape the metacharacter with a backslash (\). (See also Escaped Characters) Escaping characters can be inconvenient to use when the command line contains several metacharacters that need to be escaped.

  2. Use single quotes (' ') around a string. Single quotes protect all characters except the backslash (\).

  3. Use double quotes (" "). Double quotes protect all characters except the backslash (\), dollar sign ($) and grave accent (`).

    Double quotes is often the easiest to use because we often want environment variables to be expanded.


Single and double quotes protect each other. For example:

$ echo 'Hi "Intro to Unix" Class'
Hi "Intro to Unix" Class

$ echo "Hi 'Intro to Unix' Class"
Hi 'Intro to Unix' Class