Bash 动态变量冲突



我在下面的代码中遇到了问题,set -x 告诉我正在分配变量,但试图在此循环之外回显它们似乎不起作用?

          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}"

看起来您的代码被截断了,但这听起来像是经典的管道读取问题。

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

基本上,管道创建一个隐式子壳。要避免它,请避开管道:

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

或者显式创建子外壳,以便您可以控制范围:

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

相关内容

最新更新