更新时间:2023-12-04 13:40:28
您可以使用所谓的 ANSI -C用 $'...'
引用。 它最初是 ksh93 功能,但现在可以在 bash,zsh,mksh,FreeBSD sh 和 busybox的灰中使用(但只有在使用ENABLE_ASH_BASH_COMPAT编译时才可以。)
为默认情况下,RUN使用 / bin / sh
作为外壳程序,您需要先使用SHELL指令切换到bash之类的方式。 / p>
以 $'
开始命令,以'
结束命令>并使用 \n\
换行,例如:
SHELL [ / bin / bash, -c]
RUN echo $'[repo] \n\
名称= YUM存储库\n\
baseurl = https://example.com/packages/已启用
= 1已启用
gpgcheck = 0'> /etc/yum.repos.d/Repo.repoxyz
I want to write the following RUN command in the Dockerfile. But, docker is not preserving the new lines.
RUN echo "[repo] \
name = YUM Repository \
baseurl = https://example.com/packages/ \
enabled = 1 \
gpgcheck = 0" > /etc/yum.repos.d/Repo.repoxyz
I know that \
at the end of each line escapes the new line. But, is there any way that I can write multiple lines preserving the new line?
You can use what is called "ANSI-C quoting" with $'...'
. It was originally a ksh93 feature but it is now available in bash, zsh, mksh, FreeBSD sh and in busybox's ash (but only when it is compiled with ENABLE_ASH_BASH_COMPAT).
As RUN uses /bin/sh
as shell by default you are required to switch to something like bash first by using the SHELL instruction.
Start your command with $'
, end it with '
and use \n\
for newlines, like this:
SHELL ["/bin/bash", "-c"]
RUN echo $'[repo] \n\
name = YUM Repository \n\
baseurl = https://example.com/packages/ \n\
enabled = 1 \n\
gpgcheck = 0' > /etc/yum.repos.d/Repo.repoxyz