小编Gar*_*uth的帖子

将命令的返回码分配给 Bash 中的变量

我有一个简单的(甚至愚蠢的:)问题:在bash脚本中,我试图使用将命令的返回代码存储到变量中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
Run Code Online (Sandbox Code Playgroud)

但我得到的不是 0(成功返回代码)而是 S?:

trying to store this command return code. it should be 0
S?
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?有任何想法吗?

先感谢您。

bash

1
推荐指数
1
解决办法
6030
查看次数

标签 统计

bash ×1