Linux 中的嵌套变量替换和预定义 BASH 变量 - 第 11 部分
关于 BASH Shell 的最后两篇文章,我们详细讨论了变量,受到了读者的高度赞赏。我们作为Tecmint-Team非常热衷于为您提供最新的、最新的和详细的相关主题。此外,我们总是试图触及相应主题的主要观点。
这是关于 Linux 变量的最后一篇文章,在结束本主题之前,我们将看到变量替换和 Shell 中定义的变量。
Bash 在命令真正执行之前执行变量替换。 Linux Bash Shell 在执行命令之前会搜索所有“$”符号,并将其替换为变量的值。 Bash 变量替换过程仅执行一次。如果我们有嵌套变量怎么办?
注意:嵌套变量是指在变量内部声明的变量。让我们在下面的示例中看看上述场景。
声明一个只读且可执行的变量,如下所示。
avi@localhost:~$ declare -rx Linux_best_website="linux教程"
检查存储的变量的值。
avi@localhost:~$ printf "%s" "$Linux_best_website"
linux教程
现在声明另一个变量,该变量又是只读且可执行的。
avi@localhost:~$ declare -rx Linux_website="Linux_best_website"
现在的情况是,我们定义了两个变量。
‘Linux_best_website’,其值为“www.howtoing.com”
以及‘Linux_website’,其值为“Linux_best_website ”
如果我们运行下面的一行命令,结果会是什么?
avi@localhost:~$ printf "%s" "$Linux_website"
它应该首先用值“Linux_best_website”替换变量“$Linux_website”,然后“$Linux_best_website”又是一个变量,其值是这是“www.howtoing.com”。所以运行以下命令的最终输出应该是。
avi@localhost:~$ printf "%s" "$Linux_website"
linux教程
但不幸的是,情况并非如此,我们得到的输出是Linux_best_website。
原因?是的! Bash 仅替换变量的值一次。对于需要频繁替换变量并且需要多次替换变量的复杂脚本和程序呢?
这里出现了命令“eval”,它在脚本中多次执行变量替换的额外工作。这是一个让整个工作像玻璃一样清晰的例子。
声明一个变量x,其值为10。
avi@localhost:~/Desktop$ declare x=10
检查我们刚刚定义的变量x的值。
avi@localhost:~/Desktop$ echo $yx
x10
声明一个变量y,其值为x。
avi@localhost:~/Desktop$ declare y=x
检查我们刚刚定义的变量 y 的值。
avi@localhost:~/Desktop$ echo $y
x
这是 BASH 一次性变量替换的问题,它不执行额外一轮的变量替换。我们正在使用“eval”命令来解决此问题。
avi@localhost:~/Desktop$ eval y=$x
现在检查变量“y”的值。
avi@localhost:~/Desktop$ echo $y
10
欢呼!问题已解决,“eval”命令赢得了比赛:)
更不用说,'eval'命令在大型脚本程序中非常有帮助,是一个非常方便的工具。
这篇文章的最后但并非最不重要的部分是 BASH 预定义变量。不!看到这个列表不要惊慌。在开始编写脚本之前,除了少数几个之外,您永远不需要记住整个列表。作为学习过程的一部分,我们将介绍 BASH 预定义变量列表。
No. | BASH VARIABLE | RESULT |
1 | auto_resume | Process command completion for the stopped jobs. |
2 | BASH | PATH of the shell. |
3 | BASH_ENV | Shows the name of the profile file |
4 | BASH_VERSION | Shows the version of Bash |
5 | BASH_VERSINFO | Shows Detailed version information. |
6 | BASH_VERSINFO[0] | The major version number (the release). |
7 | BASH_VERSINFO[1] | The minor version number (the version). |
8 | BASH_VERSINFO[2] | The patch level. |
9 | BASH_VERSINFO[3] | The build version. |
10 | BASH_VERSINFO[4] | The release status (for example, beta1 ). |
11 | BASH_VERSINFO[5] | The value of MACHTYPE . |
12 | CDPATH | List of directories separated by colon. |
13 | COLUMNS | Number of Characters per line on Standard Output. |
14 | EUID | User ID of the current user. |
15 | FCEDIT | The default text editor for the fc command. |
16 | FUNCNAME | The name of the fun |
17 | GROUPS | Groups of which the user is a Member. |
18 | HISTFILE | The file containing the command history. |
19 | HOME | The name of your home directory. |
20 | LINES | Number of horizontal lines on Standard Output. |
21 | Name of a file to check for incoming mail | |
22 | OSTYPE | Name of the operating system. |
23 | OLDPWD | The previous working directory |
24 | PWD | The current working directory |
25 | RANDOM | Prints a random number |
26 | SHELL | The preferred shell to use |
27 | TIMEFORMAT | The format for the time command. |
28 | UID | The ID of the current user |
有大量预定义 BASH 变量。我们试图列出最常用的列表。
目前为止就这样了。我会再次在这里发表另一篇有趣的文章。在此之前,请继续关注并连接到 TecMint。不要忘记在下面的评论部分向我们提供您的宝贵反馈。