![linux bash 变量](https://i-blog.csdnimg.cn/blog_migrate/f932c6c26c49b2c53b089f8dfc3570ec.png)
linux bash 变量
Bash shell provides environment and shell variables in order to hold data. These variables helps system administrators during the operations by giving clues and helpers. echo
command is used to put these variables to the standard output but in some cases some characters may misinterpreted and not work. In this tutorial we will look how to print bash variables accordingly with echo
command.
Bash Shell提供环境和Shell变量以保存数据。 这些变量通过提供线索和帮助者在操作过程中帮助系统管理员。 echo
命令用于将这些变量放入标准输出,但是在某些情况下,某些字符可能会被误解并且不起作用。 在本教程中,我们将研究如何使用echo
命令相应地打印bash变量。
回声问题 (Echo Problem)
The problem is that when we provide the variable and the variables have special characters like line feeds, asterisk, square brackets, it misbehaves like below.
问题在于,当我们提供变量并且变量具有特殊字符(例如换行符,星号,方括号)时,它的行为如下所示。
$ test="This is *"
and than print with echo
比echo
打印
$ echo $test
![Echo Problem](https://i-blog.csdnimg.cn/blog_migrate/0021b0d3459a613a434cc7429b57ca97.png)
回声重击变量(Echo Bash Variables)
The most popular use case for echo
command is printing bash variables. We can use " "
double quotes where special characters will printed accordingly too.
echo
命令最流行的用例是打印bash变量。 我们也可以使用双引号" "
,其中也会相应地打印特殊字符。
$ myvar="This will printed accordingly"
$ echo "$myvar"
![Echo Bash Variables](https://i-blog.csdnimg.cn/blog_migrate/88044f27b916909eced5ff89f29225fe.png)
回声环境变量(Echo Environment Variable)
We can also use this for shell variables. In this example we will print SSH_CLIENT
variable.
我们也可以将其用于shell变量。 在此示例中,我们将打印SSH_CLIENT
变量。
$ echo "$SSH_CLIENT"
![Echo Environment Variable](https://i-blog.csdnimg.cn/blog_migrate/502c663fabea0940cd3c8683e7b78f99.png)
回音标签(Echo Tabs)
While setting variables value we can use tabs
and spaces
. We if we try to echo this variable we will loose tabs
and spaces
. In order to prevent this we can use double quotes like below.
在设置变量值时,我们可以使用tabs
和spaces
。 如果我们尝试回显此变量,我们将丢失tabs
和spaces
。 为了防止这种情况,我们可以使用双引号,如下所示。
$ var=" title | count"
$ echo "$var"
![Echo Tabs](https://i-blog.csdnimg.cn/blog_migrate/0db4d5bb3897e236f5bae2e90fa45c79.png)
回声星号(Echo Asterisks)
Asterisk is meaningful operator for Linux bash. If we are using asterisks in our variables this may create misbehavior. In order to prevent this we should use double quotes like below.
星号对于Linux bash是有意义的运算符。 如果我们在变量中使用星号,则可能会导致行为异常。 为了防止这种情况,我们应该使用如下双引号。
$ var="This is *"
$ echo "$var"
![Echo Asterisks](https://i-blog.csdnimg.cn/blog_migrate/d322b89cb6018bb2ea210dfe91999661.png)
As we can see from examples *
will list current working directories if we do not use double quotes.
从示例中可以看到,如果不使用双引号, *
将列出当前的工作目录。
翻译自: https://www.poftut.com/linux-echo-bash-variables-accordingly-with-special-characters/
linux bash 变量