Linux 无论如何从命令行获取c程序的返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8230265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anyway to get return value of c program from command line?
提问by Brandon Yates
I understand if I write a bash script I can get the return value, but is there anyway to get the return value without scripting, and just command line?
我知道如果我编写一个 bash 脚本,我可以获得返回值,但是无论如何都可以在没有脚本的情况下获取返回值,而只是命令行?
采纳答案by jweyrich
Yes, the same way you'd do in a Bash script. Run your program like this:
是的,就像您在 Bash 脚本中所做的那样。像这样运行你的程序:
./your_program; echo $?
回答by gspr
In light of the invalidation of the previous answer (good point, Carl Norum), let me re-phrase my comment as an answer:
鉴于之前的答案无效(好点,Carl Norum),让我将我的评论重新表述为答案:
BASH stores the return value of the previously run command in the variable $?
. This is independent of the programming langauge used to write said command (the command can also be a shell internal).
BASH 将先前运行的命令的返回值存储在变量中$?
。这与用于编写所述命令的编程语言无关(该命令也可以是 shell 内部的)。