如何在 heredoc 的注释中嵌入字符串?

如何在 heredoc 的注释中嵌入字符串?

这不起作用,注释不包含 IP_ADDR 字符串

SSH_UN='user'
IP_ADDR='192.168.1.101'

cat <<"EOF" >> .ssh/config
# VirtualBox (VB) on user's laptop at $IP_ADDR
Host laptopvb
  Hostname $IP_ADDR
  User $SSH_UN
  ForwardAgent yes
EOF

答案1

EOF删除:周围的双引号

IP_ADDR='192.168.1.101'
cat <<EOF >> .ssh/config
# VirtualBox (VB) on hobs laptop at $IP_ADDR
Host laptopvb # manually set as a static IP on the VB
  Hostname $IP_ADDR
  User $SSH_UN
  ForwardAgent yes
EOF

根据bash 手册

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 characters 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.  

相关内容