[英]Assign command's return code to variable in Bash
我有一个简单的(甚至很愚蠢的)问题:在bash脚本中,我试图使用S?
将命令的返回代码存储到变量中S?
,以便稍后在脚本中使用它,但是S?
存储为S? 。
我运行以下测试bash脚本:
#!/bin/bash
echo "trying to store this command's return code. it should be 0"
rtrn_code=S?
echo $rtrn_code
但是我得到S而不是得到0(成功的返回码)? :
trying to store this command return code. it should be 0
S?
我究竟做错了什么? 有任何想法吗?
先感谢您。
是$?
而不是S?
。
rtrn_code=$?
可以使用$?
访问最后执行的命令的返回值$?
。 取代S?
到$?
在您的脚本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.