分享

Shell Metacharacters — Introduction to Unix Study Guide

 WUCANADA 2014-02-16

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.

Symbol Meaning
> Output 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 Process
# Comment
$ Expand the value of a variable
\ Prevent or escape interpretation of the next character
<< Input redirection (see Here Documents)

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 Charaters) 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.

Note

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多