JsprX
  • Archive

Bash中'<<'的用法

  • Shell
  • Linux
  • Jan 02, 2021
  • 0 views
This  type  of  redirection  instructs the shell to read input from the urrent source 
until a line containing only word (with no trailing blanks) is seen. 
All of the lines read up to that point are then used as the standard input for a command.

       The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

       No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word.  If  any  charac-
       ters  in  word  are  quoted,  the  delimiter  is  the result of quote removal on word, and the lines in the here-document are not
       expanded.  If word is unquoted, all lines of the here-document are subjected to parameter expansion,  command  substitution,  and
       arithmetic  expansion.  In the latter case, the character sequence \<newline> is ignored, and \ must be used to quote the charac-
       ters \, $, and `.

       If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing  delim-
       iter.  This allows here-documents within shell scripts to be indented in a natural fashion.

以上是 man

EOF多行写入文件防止变量替换 问题描述#   对多个变量及多行输出到文件,存在变量自动替换,当使用cat«EOF不想对内容进行变量替换、命令替换、参数展开等

问题解决#   转义特殊字符如 $ `等

一、对 $·\ 进行转义

cat >> a.sh << EOF
echo \`hostname\`
echo $HOME
EOF

二、在分界符EOF前添加反斜杠\,或者用单引号、双引号括起来

cat >> a.sh << \EOF
echo `hostname`
echo $HOME
EOF
cat >> a.sh << "EOF"
echo `hostname`
echo $HOME
EOF
cat >> a.sh << 'EOF'
echo `hostname`
echo $HOME
EOF
Last updated May 16, 2021

Weijie Xu

Subscribe

This work is licensed under a Attribution-NoDerivatives 4.0 International license. Attribution-NoDerivatives 4.0 International

PREVIOUS编写卓越的代码——防御性编程
NEXTParallelStream 的正确打开方式
Search