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

[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也发生了转换.

posted @ 2017-11-20 10:57  lfree  阅读(119)  评论(0编辑  收藏  举报