我的一位同事为我提供了一种我不熟悉的 Bash 语法。我的 Google foo 无法弄清楚它的作用以及为什么/何时应该使用它。
他发给我的命令是这样的:
someVariable=something command
Run Code Online (Sandbox Code Playgroud)
最初,我认为这等效于以下内容:
someVariable=something ; command
Run Code Online (Sandbox Code Playgroud)
或者
someVariable=something
command
Run Code Online (Sandbox Code Playgroud)
但事实并非如此。例子:
[Jan-03 11:26][~]$ # Look at the environment variable BAZ. It is currently empty
[Jan-03 11:26][~]$ echo $BAZ
[Jan-03 11:27][~]$ # Try running a command of the same format
[Jan-03 11:27][~]$ BAZ=jake echo $BAZ
[Jan-03 11:27][~]$
[Jan-03 11:27][~]$ # Now, echo BAZ again. It is still empty:
[Jan-03 11:27][~]$ echo $BAZ
[Jan-03 11:27][~]$
[Jan-03 11:28][~]$
[Jan-03 11:28][~]$ # If we add a semi-colon to …Run Code Online (Sandbox Code Playgroud)