且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[20171120]bash使用here documents的细节

更新时间:2022-08-15 08:29:13

[20171120]bash使用here documents的一个小细节.txt

--//昨天看bash文档,,发现一些小细节,做一个记录,就是EOF加引号的问题.

command <<'EOF'
cmd1
cmd2 arg1
$var won't expand as parameter substitution turned off by single quoting
EOF

--//例子:

$ cat a.sh
#! /bin/bash
cat <<'EOF'
this is a test
hostname is $HOSTNAME
$(date)
EOF

$ . a.sh
this is a test
hostname is $HOSTNAME
$(date)

--//你可以发现$HOSTNAME,$(date)并没有展开或者执行转换.
--//如果写成如下:

cat <<EOF
this is a test
hostname is $HOSTNAME
$(date)
EOF

$ . a.sh
this is a test
hostname is xxxxxx
Mon Nov 20 09:22:06 CST 2017

--//可以发现现在是执行了里面的date命令,$HOSTNAME也发生了转换.