bash从这里运行脚本doc [英] bash run script from here doc
本文介绍了bash从这里运行脚本doc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,变量X正常输出.
In the code below, variable X is output normally.
# cat a.sh
X=world
echo 'hello' $X
# cat a.sh | bash
hello world
但是,在此处使用doc时,不会显示变量X.
But, using here doc, variable X is not displayed.
# cat <<EOF | bash
> X=world
> echo 'hello' $X
> EOF
hello
# bash -s <<EOF
> X=world
> echo 'hello' $X
> EOF
hello
是什么导致了这种不同?
What made this difference?
推荐答案
您可以看到删除|bash
X=oldvalue
cat <<EOF
X=world
echo "hello $X"
EOF
$X
在替换为bash之前已被替换.
您可以检查以下内容
The $X
is replaced before piping it to bash.
You can check the following
X=oldvalue
cat <<"EOF"
X=world
echo "hello $X"
EOF
这是您要执行的操作:
cat <<"EOF" | bash
X=world
echo "hello $X"
EOF
这篇关于bash从这里运行脚本doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文