heredoc\here文档\hereis在linux的使用

here文档,即here document,它有几个名称如:heredoc、hereis、here字符串或here脚本等,它可以在命令行shell和程序语言中使用。

通常多行或带缩进的字符可以用到here文档,一些程序语言可以在字符串里执行变量替换和命令替换。

语法:

here文档最通用的语法是 <<紧跟一个标识字符串,通常为大写字母,如EOF,然后下一行为字符串的内容,最后单独一行的EOF作为结尾,表示标识字符串的关闭。

标识符不一定是EOF,也可以是STREOTINDENT等字符串,但结尾行的标识符要和开始的对应。

*nix里,here文档通常用来给命令提供输入内容。

<<EOF
line one
line two
line three
EOF
示例:
$ cat <<EOF
working in $PWD
EOF

执行会输出

working in /root/

可以用单引号'或双引号"来包裹标识符

$ cat <<"EOF"
working in $PWD
EOF

$ cat <<'EOF'
working in $PWD
EOF

以下会输出

working in $PWD
<<-EOF
    line one
    line two
    line three
EOF
实用技巧:
cat <<'EOF' >/etc/yum.repos.d/Centos-7-aliyun.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF

以上示例是将阿里云的centos-7-aliyun.repo内容覆盖到/etc/yum.repos.d/Centos-7-aliyun.repo文件,该文件之前的内容将会被覆盖,由于文本中存在$releasever等变量,为了防止heredoc转换这些变量的内容,EOF需要用引号包围变成'EOF'

追加文件内容

cat <<'EOF' >>/etc/vimrc
set ts=4
set expandtab
EOF

以上示例是将vim的两个指令追加到/etc/vimrc文件中,指令内容会在文件的最底部追加两行,不会覆盖原文件。

参考:

https://ruby-china.org/topics/25983

https://zh.wikipedia.org/zh-cn/Here%E6%96%87%E6%A1%A3

https://juejin.cn/post/6844903854023114760