Unix 简明教程

Unix / Linux - Shell Substitution

What is Substitution?

当 Shell 遇到包含一个或多个特殊字符的表达式时,它会执行替换。

The shell performs substitution when it encounters an expression that contains one or more special characters.

Example

此处,变量的打印值被替换为其值。同时, "\n" 被替换为换行符 −

Here, the printing value of the variable is substituted by its value. Same time, "\n" is substituted by a new line −

#!/bin/sh

a=10
echo -e "Value of a is $a \n"

您会收到以下结果。在此处 -e 选项允许解释反斜杠转义符。

You will receive the following result. Here the -e option enables the interpretation of backslash escapes.

Value of a is 10

以下是没有 -e 选项的结果 −

Following is the result without -e option −

Value of a is 10\n

可以在 echo 命令中使用的以下转义序列 −

The following escape sequences which can be used in echo command −

Sr.No.

Escape & Description

1

\\ backslash

2

\a alert (BEL)

3

\b backspace

4

*\c * suppress trailing newline

5

*\f * form feed

6

\n new line

7

\r carriage return

8

*\t * horizontal tab

9

*\v * vertical tab

您可以使用 -E 选项禁用对反斜杠转义符的解释(默认)。

You can use the -E option to disable the interpretation of the backslash escapes (default).

您可以使用 -n 选项禁用插入换行符。

You can use the -n option to disable the insertion of a new line.

Command Substitution

命令替换是一种机制,通过该机制,Shell 执行一组给定的命令,然后用这些命令的输出替换命令。

Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the place of the commands.

Syntax

当给出命令时,执行命令替换 −

The command substitution is performed when a command is given as −

`command`

在执行命令替换时,请确保使用反引号,而不是单引号字符。

When performing the command substitution make sure that you use the backquote, not the single quote character.

Example

命令替换通常用于将命令的输出分配给变量。以下每个示例都展示了命令替换 −

Command substitution is generally used to assign the output of a command to a variable. Each of the following examples demonstrates the command substitution −

#!/bin/sh

DATE=`date`
echo "Date is $DATE"

USERS=`who | wc -l`
echo "Logged in user are $USERS"

UP=`date ; uptime`
echo "Uptime is $UP"

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

Upon execution, you will receive the following result −

Date is Thu Jul  2 03:59:57 MST 2009
Logged in user are 1
Uptime is Thu Jul  2 03:59:57 MST 2009
03:59:57 up 20 days, 14:03,  1 user,  load avg: 0.13, 0.07, 0.15

Variable Substitution

变量替换使 Shell 程序员能够根据变量的状态来操作变量的值。

Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.

以下是所有可能替换操作的表格 −

Here is the following table for all the possible substitutions −

Sr.No.

Form & Description

1

${var} Substitute the value of var.

2

${var:-word} If var is null or unset, word is substituted for var. The value of var does not change.

3

${var:=word} If var is null or unset, var is set to the value of word.

4

${var:?message} If var is null or unset, message is printed to standard error. This checks that variables are set correctly.

5

${var:+word} If var is set, word is substituted for var. The value of var does not change.

Example

以下示例显示了上述替换的各种状态 −

Following is the example to show various states of the above substitution −

#!/bin/sh

echo ${var:-"Variable is not set"}
echo "1 - Value of var is ${var}"

echo ${var:="Variable is not set"}
echo "2 - Value of var is ${var}"

unset var
echo ${var:+"This is default value"}
echo "3 - Value of var is $var"

var="Prefix"
echo ${var:+"This is default value"}
echo "4 - Value of var is $var"

echo ${var:?"Print this message"}
echo "5 - Value of var is ${var}"

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

Upon execution, you will receive the following result −

Variable is not set
1 - Value of var is
Variable is not set
2 - Value of var is Variable is not set

3 - Value of var is
This is default value
4 - Value of var is Prefix
Prefix
5 - Value of var is Prefix