Unix 简明教程

Unix / Linux - Shell Quoting Mechanisms

在本章中,我们将详细讨论 Shell 引用机制。我们将从讨论元字符开始。

In this chapter, we will discuss in detail about the Shell quoting mechanisms. We will start by discussing the metacharacters.

The Metacharacters

Unix Shell 提供了各种元字符,在任何 Shell 脚本中使用它们时都具有特殊含义,并且会导致某个单词终止,除非加引号。

Unix Shell provides various metacharacters which have special meaning while using them in any Shell Script and causes termination of a word unless quoted.

例如, ? 与目录中列出的单个字符匹配,而 * 与多个字符匹配。以下是大多数 shell 特殊字符(也称为元字符)的列表 −

For example, ? matches with a single character while listing files in a directory and an * matches more than one character. Here is a list of most of the shell special characters (also called metacharacters) −

* ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab

通过在字符前加上 \ 可以引用一个字符(即,使它代表自身)。

A character may be quoted (i.e., made to stand for itself) by preceding it with a \.

Example

以下示例显示了如何打印 *?

Following example shows how to print a * or a ?

#!/bin/sh

echo Hello; Word

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

Hello
./test.sh: line 2: Word: command not found

shell returned 127

我们现在尝试使用一个带引号的字符 −

Let us now try using a quoted character −

#!/bin/sh

echo Hello\; Word

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

Hello; Word

$ 符号是元字符之一,因此必须加上引号以避免 shell 对其进行特殊处理 −

The $ sign is one of the metacharacters, so it must be quoted to avoid special handling by the shell −

#!/bin/sh

echo "I have \$1200"

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

I have $1200

下表列出了四种引用形式 −

The following table lists the four forms of quoting −

Sr.No.

Quoting & Description

1

Single quote All special characters between these quotes lose their special meaning.

2

Double quote Most special characters between these quotes lose their special meaning with these exceptions − $`\$\'\"\\

3

Backslash Any character immediately following the backslash loses its special meaning.

4

Back quote Anything in between back quotes would be treated as a command and would be executed.

The Single Quotes

考虑一个包含许多特殊 shell 字符的 echo 命令−

Consider an echo command that contains many special shell characters −

echo <-$1500.**>; (update?) [y|n]

在每个特殊字符前放置反斜杠既繁琐又增加了阅读的难度−

Putting a backslash in front of each special character is tedious and makes the line difficult to read −

echo \<-\$1500.\*\*\>\; \(update\?\) \[y\|n\]

有一种简单的方法可以引用一大组字符。在字符串的开头和结尾处放置单引号(')−

There is an easy way to quote a large group of characters. Put a single quote (') at the beginning and at the end of the string −

echo '<-$1500.**>; (update?) [y|n]'

单引号中的字符就像在每个字符前加上反斜杠一样被引用。使用这种方法,echo 命令可以正常显示。

Characters within single quotes are quoted just as if a backslash is in front of each character. With this, the echo command displays in a proper way.

如果字符串中出现单引号需要输出,你不应该将整个字符串放在单引号中,而应在该字符前加上反斜杠(\),如下所示−

If a single quote appears within a string to be output, you should not put the whole string within single quotes instead you should precede that using a backslash (\) as follows −

echo 'It\'s Shell Programming

The Double Quotes

尝试执行以下 shell 脚本。此 shell 脚本使用了单引号−

Try to execute the following shell script. This shell script makes use of single quote −

VAR=ZARA
echo '$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]'

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]

这不是预期要显示的内容。很明显,单引号可以防止变量替换。如果你希望替换变量值并让引号正常工作,那么你需要将命令放入双引号中,如下所示−

This is not what had to be displayed. It is obvious that single quotes prevent variable substitution. If you want to substitute variable values and to make inverted commas work as expected, then you would need to put your commands in double quotes as follows −

VAR=ZARA
echo "$VAR owes <-\$1500.**>; [ as of (`date +%m/%d`) ]"

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

ZARA owes <-$1500.**>; [ as of (07/02) ]

双引号消除了除以下字符之外所有字符的特殊含义−

Double quotes take away the special meaning of all characters except the following −

  1. $ for parameter substitution

  2. Backquotes for command substitution

  3. \$ to enable literal dollar signs

  4. \` to enable literal backquotes

  5. \" to enable embedded double quotes

  6. \\ to enable embedded backslashes

  7. All other \ characters are literal (not special)

单引号中的字符就像在每个字符前加上反斜杠一样被引用。这有助于 echo 命令正确显示。

Characters within single quotes are quoted just as if a backslash is in front of each character. This helps the echo command display properly.

如果字符串中出现单引号需要输出,你不应该将整个字符串放在单引号中,而应在该字符前加上反斜杠(\),如下所示−

If a single quote appears within a string to be output, you should not put the whole string within single quotes instead you should precede that using a backslash (\) as follows −

echo 'It\'s Shell Programming'

The Backquotes

将任何 Shell 命令放入 backquotes 中会执行该命令。

Putting any Shell command in between backquotes executes the command.

Syntax

以下是将任何 Shell command 放入反引号中的简单语法−

Here is the simple syntax to put any Shell command in between backquotes −

var=`command`

Example

在以下示例中, date 命令被执行,而产生的结果存储在 DATA 变量中。

The date command is executed in the following example and the produced result is stored in DATA variable.

DATE=`date`

echo "Current Date: $DATE"

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

Current Date: Thu Jul  2 05:28:45 MST 2009