Unix 简明教程
Unix / Linux - Using Shell Variables
在本章中,我们将学习如何在 Unix 中使用 Shell 变量。变量是一个字符字符串,我们为其分配一个值。分配的值可以是数字、文本、文件名、设备或任何其他类型的数据。
In this chapter, we will learn how to use Shell variables in Unix. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.
变量不过是实际数据的指针。shell 允许你创建、分配和删除变量。
A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.
Variable Names
变量的名称只能包含字母(a 到 z 或 A 到 Z)、数字(0 到 9)或下划线字符(_)。
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
根据惯例,Unix shell 变量的名称使用大写。
By convention, Unix shell variables will have their names in UPPERCASE.
以下示例是有效的变量名称 −
The following examples are valid variable names −
_ALI
TOKEN_A
VAR_1
VAR_2
以下是无效变量名称的示例 −
Following are the examples of invalid variable names −
2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!
你无法使用 ! 、 * 或 - 等其他字符的原因是,这些字符对 shell 具有特殊含义。
The reason you cannot use other characters such as !, *, or - is that these characters have a special meaning for the shell.
Defining Variables
变量的定义如下 −
Variables are defined as follows −
variable_name=variable_value
例如 -
For example −
NAME="Zara Ali"
以上示例定义了变量 NAME 并为其分配值 "Zara Ali"。此类型的变量称为 scalar variables 。标量变量一次只能保存一个值。
The above example defines the variable NAME and assigns the value "Zara Ali" to it. Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.
Shell 允许你将任何值存储在变量中。例如 −
Shell enables you to store any value you want in a variable. For example −
VAR1="Zara Ali"
VAR2=100
Accessing Values
要访问存储在变量中的值,请在其名称前加上美元符号 ( $ ) −
To access the value stored in a variable, prefix its name with the dollar sign ($) −
例如,以下脚本将访问定义的变量 NAME 的值并将其打印到 STDOUT −
For example, the following script will access the value of defined variable NAME and print it on STDOUT −
#!/bin/sh
NAME="Zara Ali"
echo $NAME
以上脚本将生成以下值 −
The above script will produce the following value −
Zara Ali
Read-only Variables
Shell 通过使用只读命令提供了一种将变量标记为只读的方法。变量标记为只读后,其值无法更改。
Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed.
例如,以下脚本在尝试更改 NAME 的值时生成错误 −
For example, the following script generates an error while trying to change the value of NAME −
#!/bin/sh
NAME="Zara Ali"
readonly NAME
NAME="Qadiri"
上述脚本会生成以下结果 -
The above script will generate the following result −
/bin/sh: NAME: This variable is read only.
Unsetting Variables
取消或删除变量会指示 shell 从其跟踪的变量列表中删除该变量。取消变量后,你将无法访问变量中存储的值。
Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.
以下是使用 unset 命令取消已定义变量的语法 −
Following is the syntax to unset a defined variable using the unset command −
unset variable_name
以上命令取消已定义变量的值。以下是一个简单的示例,演示了该命令如何工作 −
The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works −
#!/bin/sh
NAME="Zara Ali"
unset NAME
echo $NAME
以上示例不打印任何内容。你无法使用 unset 命令取消标记为 readonly 的 unset 变量。
The above example does not print anything. You cannot use the unset command to unset variables that are marked readonly.
Variable Types
当 shell 运行时,存在三种主要类型的变量 −
When a shell is running, three main types of variables are present −
-
Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
-
Environment Variables − An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.
-
Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.