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