(=) 符号后有空格的变量赋值? [英] Assignment of variables with space after the (=) sign?

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

问题描述

在 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中,变量PWD被设置为空字符串在执行命令 /bin/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<引起的问题/code> 被其他地方覆盖.

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天全站免登陆