0

我在下面的代码中遇到问题, 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}"
4

1 回答 1

2

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

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

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

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
)
于 2013-10-29T16:51:19.713 回答