linux bash 变量_带有特殊字符Linux Echo Bash变量

linux bash 变量

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 echocommand.

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
Echo Problem
回声问题

回声重击变量(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
Echo Bash Variables
回声重击变量

回声环境变量(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
Echo Environment Variable
回声环境变量

回音标签(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.

在设置变量值时,我们可以使用tabsspaces 。 如果我们尝试回显此变量,我们将丢失tabsspaces 。 为了防止这种情况,我们可以使用双引号,如下所示。

$ var=" title | count"
$ echo "$var"
Echo Tabs
Echo Tabs
回音标签

回声星号(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
Echo Asterisks
回声星号

As we can see from examples * will list current working directories if we do not use double quotes.

从示例中可以看到,如果不使用双引号, *将列出当前的工作目录。

LEARN MORE  How To Replace A Text Exists In Many Files?
了解更多信息如何替换许多文件中存在的文本?

翻译自: https://www.poftut.com/linux-echo-bash-variables-accordingly-with-special-characters/

linux bash 变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值