「EOF」で記述されていない変数

「EOF」で記述されていない変数

これは私のスクリプトです。

var="lalallalal"
tee file.tex <<'EOF'
text \\ text \\
$var
EOF

「EOF」(引用符を含む)を使用する必要があります。それ以外の場合は、二重スラッシュ()を使用できないためです//

ただし、引用符を使用すると、$var変数は拡張されません。

ベストアンサー1

見つかったようにEOF変数を引用すると、変数は拡張されません。これについては、次のドキュメントセクションで説明していますman bash

   No parameter and variable expansion, command  substitution,  arithmetic
   expansion,  or pathname expansion is performed on word.  If any part of
   word is 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, the charac‐
   ter sequence \<newline> is ignored, and \ must be  used  to  quote  the
   characters \, $, and `.

そこに説明されているもう一つのことは、 を使い続けることができるということです。\単にエスケープするだけです\\。したがって、両方が必要なので、\それぞれをエスケープする必要があります\\\\。これらすべてを総合すると、次のようになります。

#!/bin/bash
var="lalallalal"
tee file.tex <<EOF
text \\\\ text \\\\
$var
EOF

おすすめ記事