小编BAS*_*ASH的帖子

bash 函数在 $() 调用时无法更改局部变量的值

根据调用 Parse_Short_Switches() 的方式,以下代码的工作方式有所不同。使用 result=$(Parse_Short_Switches) 调用函数“Parse_Short_Switches”后,$error_flag 的值并未像我预期的那样设置为 -1。

通过简单地使用 Parse_Short_Switches 调用函数“Parse_Short_Switches”后,$error_flag 的值按照我的预期设置为 -1。

任何帮助将不胜感激。谢谢

#!/bin/bash

function Parse_Short_Switches()
{
echo "error_flag inside of the function= $error_flag" 1>&2
error_flag="-1"
echo "blah blah ..."
}

# --- MAIN ---
error_flag="999"
echo "error_flag= $error_flag"
#result=$(Parse_Short_Switches)
Parse_Short_Switches
echo "error_flag= $error_flag"
Run Code Online (Sandbox Code Playgroud)

bash scripts functions

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

标签 统计

bash ×1

functions ×1

scripts ×1