文章目录
  1. 1. Here Documents基本用法
  2. 2. 忽略前导的tab符号
  3. 3. 禁止变量替换
  4. 4. 多行注释
  5. 5. Reference

Here Documents基本用法

Here Document是一种特殊的代码块, 通常作为一种I/O重定向的方式给命令提供输入
格式如下:

1
2
3
4
5
COMMAND <<InputComesFromHERE
...
...
...
InputComesFromHERE

以”<< delimiter”的形式标识多行字符串的开始,之后新起一行包含相应的文本,
最后以相同的delimiter独占一行,标识多行字符串的结束。

定界符(delimiter)可以是任意的字符串

例1: 给tr命令提供输入

1
2
3
4
5
6
$ tr a-z A-Z << EOF
> one two three
> uno ods tres
> EOF
ONE TWO THREE
UNO ODS TRES

例2:非交互式地使用vim
模拟sed的用法,在输入文件中插入两行文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#/bin/sh
# Noninteractive use of 'vi' to edit a file.
# Emulates 'sed'.
E_BADARGS=85
if [ -z "$1" ]
then
echo "Usage: `basename $0` filename"
exit $E_BADARGS
fi
TARGETFILE=$1
# Insert 2 lines in file, then save.
#--------Begin here document-----------#
vim $TARGETFILE <<x23LimitStringx23
i
This is line 1 of the example file.
This is line 2 of the example file.

ZZ
x23LimitStringx23
#----------End here document-----------#
# Note that ^[ above is a literal escape
#+ typed by Control-V <Esc>.
# Bram Moolenaar points out that this may not work with 'vim'
#+ because of possible problems with terminal interaction.
exit

例3:非交互式地使用ex
将所有txt文件中的Smith替换为Jones

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Replace all instances of "Smith" with "Jones"
#+ in files with a ".txt" filename suffix.
ORIGINAL=Smith
REPLACEMENT=Jones
for file in $(fgrep -l $ORIGINAL *.txt)
do
# -------------------------------------
ex $file <<EOF
%s/$ORIGINAL/$REPLACEMENT/g
wq
EOF
# %s is the "ex" substitution command.
# wq is write-and-quit.
# -------------------------------------
done

例4:使用cat输出多行信息

1
2
3
4
5
6
7
8
9
cat <<End-of-message
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
This is the last line of the message.
-------------------------------------
End-of-message

如果将cat << End-of-message替换为 cat > $filename << End-of-message
那么cat将会把输出写入文件中,而不会输出到标准输出

忽略前导的tab符号

格式如下:

1
2
3
4
5
COMMAND <<-delimiter
...
...
...
delimiter

例:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
cat <<-ENDOFMESSAGE
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
This is the last line of the message.
ENDOFMESSAGE
exit 0

delimiter以 - 开头, 将会去除行首的tab符号,但不包括空格符号
注意: 对非行首的tab符号无效

禁止变量替换

默认情况下, HereDocuments支持变量替换

例1:

1
2
3
4
5
cat << EOF
current dir=${PWD}
EOF
命令输出结果如下:
current dir=/data/repo/blog

可以通过对delimiter的任何部分添加引号标签,取消变量替换

例2:

1
2
3
4
5
cat << "EOF"
current dir=${PWD}
EOF
命令输出结果如下:
current dir=${PWD}

例3:

1
2
3
4
5
cat << "E"OF
current dir=${PWD}
EOF
命令输出结果如下:
current dir=${PWD}

多行注释

使用Anonymous Here Documents对代码块进行多行注释

格式如下:

1
2
3
4
5
6
: <<COMMENTBLOCK
echo "This line will not echo."
......
......
COMMENTBLOCK

Here Document 将会创建一个临时文件,这些文件在打开就被删除,其他进程无法访问该文件

1
2
3
4
$ bash -c 'lsof -a -p $$ -d0' << EOF
EOF
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
lsof 4399 caodan 0r REG 0,36 0 64130 /tmp/sh-thd-1008598812 (deleted)


Reference

[0] The Linux Documentation Project __ Advanced Bash-Scripting Guide
Chapter19. Here Documents —Aldous Huxley, Island
http://www.tldp.org/LDP/abs/html/here-docs.html

文章目录
  1. 1. Here Documents基本用法
  2. 2. 忽略前导的tab符号
  3. 3. 禁止变量替换
  4. 4. 多行注释
  5. 5. Reference