在(=)号后是否给变量赋空格? [英] Assignment of variables with space after the (=) sign?

查看:81
本文介绍了在(=)号后是否给变量赋空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Bash中,使用T=content为变量赋值,在等号之前或之后没有空格.

In Bash, assigning values to variables is done using T=content, with no spaces before or after the equal sign.

尽管我已经在shell脚本PWD= /bin/pwd中看到了以下内容,但在等号的右侧包含一个空格.

Despite that I've seen the following in a shell script PWD= /bin/pwd containing a space on the right side of the equals sign.

它有一个空格的目的是什么?

What's the purpose of it have a space?

推荐答案

在示例PWD= /bin/pwd中,在执行命令/bin/pwd之前,将变量PWD设置为空字符串.更改仅对该行生效.

In the example PWD= /bin/pwd, the variable PWD is set to the empty string before executing the command /bin/pwd. The change only takes effect for that line.

这对于在运行命令时临时更改变量而不会影响原始值很有用.另一个示例是使用read设置其他IFS的情况:

This can be useful to make a temporary change to a variable for the purposes of running a command, without affecting the original value. Another example of this would be when using read, to set a different IFS:

IFS=, read a b c <<<"comma,separated,list"

这会将字段分隔符设置为逗号,以便正确读取abc.在此行之后,IFS返回默认值,因此脚本的其余部分不受影响.

This sets the field separator to a comma so that a, b and c are read correctly. After this line, IFS returns to the default value, so the rest of the script isn't affected.

也许在某些系统上,命令pwd的输出受变量PWD的值影响,因此这样做可以防止因PWD引起的问题在其他地方被覆盖.

Perhaps on some systems, the output of the command pwd is affected by the value of the variable PWD, so doing this prevents problems caused by PWD being overwritten elsewhere.

这篇关于在(=)号后是否给变量赋空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆