我正在玩 Bash 脚本。我想将命令的结果分配给一个变量,并将其返回代码分配给另一个变量。
前任。:
line_count=$(cat "$filename" | wc -l) #If the file does not exist, an error code is returned
cmd_return_code=$? #I want to assign the previous error code to a variable
Run Code Online (Sandbox Code Playgroud)
我尝试如上所示使用$?,但它捕获了前一个赋值本身的返回码,即 0。
我怎样才能做到这一点?
谢谢