相关疑难解决方法(0)

为什么不使用“哪个”?那用什么?

当寻找的路径,可执行文件或检查,如果你在Unix shell中输入命令的名称会发生什么,有不同的公用事业过多(whichtypecommandwhencewherewhereiswhatishash,等)。

我们经常听说which应该避免。为什么?我们应该用什么来代替?

shell history which portability

422
推荐指数
4
解决办法
7万
查看次数

这个 shell/Bash 语法是什么:someVariable=someValue someCommand

我的一位同事为我提供了一种我不熟悉的 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)

command-line shell bash environment-variables

27
推荐指数
3
解决办法
2553
查看次数

当我在命令前加上“一次性变量赋值”时,为什么 bash 不扩展这个变量

如果我运行此 bash 命令并为语句添加前缀,则该变量fruit应该存在,但仅在此命令的持续时间内:

$ fruit=apple echo $fruit

$
Run Code Online (Sandbox Code Playgroud)

结果是一个空行。为什么?

引用通配符对此问题的评论:

参数扩展由shell完成,“fruit”变量不是shell变量;它只是“echo”命令环境中的一个环境变量

环境变量仍然是一个变量,所以这肯定对 echo 命令仍然可用吗?

bash variable

12
推荐指数
2
解决办法
3101
查看次数