EOFを使用してバックスラッシュと改行文字を保存します。

EOFを使用してバックスラッシュと改行文字を保存します。

EOF次のファイルを作成しています。

cat <<EOF > Dockerfile
RUN apt-get update -y \
  && apt-get install -y \
    bsdtar \
    git \
    locales
EOF

しかし、結果は次のようになります。

RUN apt-get update -y   && apt-get install -y     bsdtar     git     locales

バックスラッシュと改行を維持したいです。

ベストアンサー1

EOFトークンを引用する必要があります。

cat <<"EOF" > Dockerfile
RUN apt-get update -y \
  && apt-get install -y \
    bsdtar \
    git \
    locales
EOF

変数も拡張するには、バックスラッシュをエスケープして引用符を使用しないでください。

該当する部分ですman bash

      [n]<<[-]word
              here-document
      delimiter

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

おすすめ記事