Bash和Docker:读取循环的奇怪herdoc行为
当使用Bash和Docker:读取循环的奇怪herdoc行为,bash,docker,heredoc,Bash,Docker,Heredoc,当使用while read循环迭代多个值时,我观察到一种奇怪的行为。奇怪的是,当我使用herdoc将代码传递到Docker容器时,读取的变量总是空的: $ docker run --rm -i ubuntu:18.04 << EOF echo -e "123\n456"|while read f; do echo "Value: $f"; done EOF Value: Value: 这里缺少什么?您的第一个“here doc”执行参数扩展,并且$f变成空字符串。为避免出现这
while read
循环迭代多个值时,我观察到一种奇怪的行为。奇怪的是,当我使用herdoc将代码传递到Docker容器时,读取的变量总是空的:
$ docker run --rm -i ubuntu:18.04 << EOF
echo -e "123\n456"|while read f; do echo "Value: $f"; done
EOF
Value:
Value:
这里缺少什么?您的第一个“here doc”执行参数扩展,并且$f
变成空字符串。为避免出现这种情况,请引用EOF
:
docker run --rm -i ubuntu:18.04 <<'EOF'
echo -e "123\n456"|while read f; do echo "Value: $f"; done
EOF
dockerrun--rm-ibuntu:18.04的确是个不错的版本。另一种选择是逃离美元。
$ docker run --rm -it ubuntu:18.04 bash
root@0d71388ad90d:/# echo -e "123\n456"|while read f; do echo "Value: $f"; done
Value: 123
Value: 456
docker run --rm -i ubuntu:18.04 <<'EOF'
echo -e "123\n456"|while read f; do echo "Value: $f"; done
EOF