(转)linux shell 的here document 用法 (cat << EOF)

什么是Here Documen:
Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下

cmd << delimiter
  Here Document Content
delimiter

它的作用就是将两个 delimiter 之间的内容(Here Document Content 部分) 传递给cmd 作为输入参数。

比如在终端中输入cat << EOF ,系统会提示继续进行输入,输入多行信息再输入EOF,中间输入的信息将会显示在屏幕上。如下:

fish@mangos:~$ cat << EOF
> First Line
> Second Line
> Third Line EOF
> EOF
First Line
Second Line
Third Line EOF

注: >这个符号是终端产生的提示输入信息的标识符

这里要注意几点

EOF 只是一个标识而已,可以替换成任意的合法字符
作为结尾的delimiter一定要顶格写,前面不能有任何字符
作为结尾的delimiter后面也不能有任何的字符(包括空格)
作为起始的delimiter前后的空格会被省略掉
Here Document 不仅可以在终端上使用,在shell 文件中也可以使用,例如下面的here.sh 文件

cat << EOF > output.sh
echo "hello"
echo "world"
EOF

使用 sh here.sh 运行这个脚本文件,会得到output.sh 这个新文件,里面的内容如下

echo "hello"
echo "world"

Here Document的变形
delimiter 与变量 在Here Document 的内容中,不仅可以包括普通的字符,还可以在里面使用变量,例如将上面的here.sh 改为

cat << EOF > output.sh
echo "This is output"
echo $1
EOF

使用sh here.sh HereDocument 运行脚本得到output.sh的内容

echo "This is output"
echo HereDocument

在这里 $1 被展开成为了脚本的参数 HereDocument

但是有时候不想展开这个变量怎么办呢,可以通过在起始的 delimiter的前后添加 " 来实现,例如将上面的here.sh 改为

cat << "EOF" > output.sh  #注意引号
echo "hello"
echo "world"
EOF

得到的output.sh 的内容为

echo "This is output"
echo $1

原文地址:https://my.oschina.net/u/1032146/blog/146941

原文地址:https://www.cnblogs.com/mkdlf/p/7445822.html