Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash和Docker:读取循环的奇怪herdoc行为_Bash_Docker_Heredoc - Fatal编程技术网

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