更新时间:2022-12-28 14:30:57
在例子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"
这将字段分隔符设置为逗号,以便正确读取 a
、b
和 c
.在这一行之后,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.