当寻找的路径,可执行文件或检查,如果你在Unix shell中输入命令的名称会发生什么,有不同的公用事业过多(which
,type
,command
,whence
,where
,whereis
,whatis
,hash
,等)。
我们经常听说which
应该避免。为什么?我们应该用什么来代替?
我的一位同事为我提供了一种我不熟悉的 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) 如果我运行此 bash 命令并为语句添加前缀,则该变量fruit
应该存在,但仅在此命令的持续时间内:
$ fruit=apple echo $fruit
$
Run Code Online (Sandbox Code Playgroud)
结果是一个空行。为什么?
引用通配符对此问题的评论:
参数扩展由shell完成,“fruit”变量不是shell变量;它只是“echo”命令环境中的一个环境变量
环境变量仍然是一个变量,所以这肯定对 echo 命令仍然可用吗?