Bash动态变量冲突

时间:2013-10-29 16:44:27

标签: bash while-loop dynamically-generated bash4

我遇到下面这段代码的问题,设置-x告诉我变量ARE被分配了,但试图在这个循环之外回显它们似乎没有用?

          export "ex_$x"=$(git rev-parse HEAD | cut -c1-10)

      done
    ((used++))

    echo $ex_render
    echo $ex_storage

    exit # =/

    php -f "${cdir}/../public/bootstrap.php" -- "${line}" "${ex_render}" "${ex_storage}"

1 个答案:

答案 0 :(得分:2)

看起来您的代码被截断了,但听起来像<{3}}

$ echo hi | read x
$ echo $x
$ # Nothing!
$ read x <<< hi
$ echo $x
hi

基本上,管道会创建一个隐式子shell。为了避免它,要么避开管道:

while read foo; do things; done < <(process substitution)

或者显式创建子shell,以便控制范围:

inputcommand | ( while read foo; do things; done;
  # variables still assigned as long as you're in the subshell
)