Nan*_*iao 12 shell bash portability
在Bash
shell中,我可以通过$?
变量获取命令退出状态:
# ps -ef | grep "haha"
root 15439 15345 0 23:02 pts/0 00:00:00 grep --color=auto haha
# echo $?
0
Run Code Online (Sandbox Code Playgroud)
它仅在 Bash shell 中可用吗?或者我也可以在其他 shell 中使用它?
apr*_*boy 13
正如 Thomas Dickey 所说,任何 POSIX shell(即几乎所有的 shell)都会有$?
.
这个问题让我很感兴趣,所以我在我能接触到的任何 shell 上测试了它:
mksh
zsh
/bin/sh
在我的三星 Galaxy S5 上/bin/sh
在我的路由器上tcsh
ksh
dash
/bin/sh
在我 1989 年左右的虚拟 UNIX System V 上cmd.exe
和powershell.exe
我的Windows 10计算机上并$?
在所有这些中工作,但是fish
和cmd.exe
。
发现两件有趣的事:
$?
在Windows PowerShell中工作!嗯,说到点子上了。而不是返回 0 或更高的数字,它只是True
and False
。
PS C:\WINDOWS\system32> echo $?
True
PS C:\WINDOWS\system32> gfdhgfhfdgfdg
gfdhgfhfdgfdg : The term 'gfdhgfhfdgfdg' is not recognized as the name of a cmdlet, ...(big long error output).....
PS C:\WINDOWS\system32> echo $?
False
Run Code Online (Sandbox Code Playgroud)
$?
在 shell 中不起作用fish
。但是,当您输入$?
fish 时,您会收到以下消息:
~$ echo $?
$? is not the exit status. In fish, please use $status.
fish: echo $?
Run Code Online (Sandbox Code Playgroud)
我没怎么用过但我并不感到惊讶,fish
似乎有自己有趣的 shell 语言,完全不同bash
或什么的。