Nan*_*iao 12 shell bash portability
在Bashshell中,我可以通过$?变量获取命令退出状态:
# 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 上测试了它:
mkshzsh/bin/sh 在我的三星 Galaxy S5 上/bin/sh 在我的路由器上tcshkshdash/bin/sh 在我 1989 年左右的虚拟 UNIX System V 上cmd.exe和powershell.exe我的Windows 10计算机上并$?在所有这些中工作,但是fish和cmd.exe。
发现两件有趣的事:
$?在Windows PowerShell中工作!嗯,说到点子上了。而不是返回 0 或更高的数字,它只是Trueand 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或什么的。
| 归档时间: |
|
| 查看次数: |
5408 次 |
| 最近记录: |