0

I would like to create a longer multiline string in Bash without variable expansion. This text contains also items like ${varname} which is expanded according to the specification. However, I don't want to expand them in my script.

Dockerfile=`cat <<____
ARG BUILD_IMAGE
FROM ${BUILD_IMAGE}
...
# lots of further lines
____
`

I tried several variants ($\{, $\0173), but I could only solve it with

b='${'
...
FROM ${b}BUILD_IMAGE}
...

Which is really ugly. Is there any better way to solve this?

Edit:

Dockerfile=`cat <<'____'
ARG BUILD_IMAGE
FROM ${BUILD_IMAGE}
...
# lots of further lines
____
`

would solve the issue, but in this case I can't use any variable expansion.

Edit 2 - Answer:

Since the post has been closed, I answer my question here:

There is a difference between using backquote and $(. The issue is solved if I use the latter one. I had the problem, because I used the former one.

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or . The first backquote not preceded by a backslash terminates the command sub‐ stitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

This means in praxis:

text=$(cat <<__EOT__
Hello ${who}, hello \${who}!
__EOT__
)
echo $text

text=`cat <<__EOT__
Hello ${who}, hello \${who}!
__EOT__
`
echo $text

results in

Hello World, hello ${who}!
Hello World, hello World!
FERcsI
  • 388
  • 1
  • 10
  • 1
    That's a here document, not a here string (those use `<<<` syntax). – tripleee Nov 06 '22 at 16:16
  • You can embed newlines in a simple single-quoted string; you don't really need to use `cat`. – chepner Nov 07 '22 at 00:58
  • @chepner, the question is not about new lines. It is about variable expansion. In a longer text. I emphasized the question. – FERcsI Nov 07 '22 at 05:28
  • They are trying to say you don't need a here document for this simple assignment. Just assign the variable with newlines between the single quotes. – tripleee Nov 07 '22 at 06:54
  • @FERcsI That's why I commented, rather than answering. The question is more or less moot. – chepner Nov 07 '22 at 13:36
  • @tripleee, thanks for the answer. The text, which I have not copied here has 20-30 lines, so heredoc is a better choice (I've been using bash for 2 decades, but not every day). I am searching for an escape sequence instead of `$b`, but I am afraid of not having any. cat << '____' would also solve the problem, but in that case none of the variables are expanded. – FERcsI Nov 08 '22 at 09:28
  • @chepner, which, according to this, was not clear, that I have a longer text, where heredoc looks to be the best choice for me (without having extra files). And I can solve this issue in multiple (not really nice) ways, but I would like to learn if there are a nice solution for escaping in here. – FERcsI Nov 08 '22 at 09:40
  • I added a few more duplicates which discuss various aspects of this. The last one has an answer of mine which demonstrates `printf` where you could then single-quote most lines, but selectively double-quote a few where you need to. – tripleee Nov 08 '22 at 09:54
  • @tripleee, thanks for the extra info. I could figure out, that backtick behaves differently from `$(`, which is actually documented. So `text=\`cat <<_EOT_` Is different from `text=$(cat <<_EOT_` And I need this latter. I thought they are the same, until now. – FERcsI Nov 09 '22 at 07:37
  • @chepner The references you sent me were helpful to find the final issue, but none of them contained the answer, so **I consider my question a new one**. Including my answer. I suggest to reopen it for others to learn from it. – FERcsI Nov 09 '22 at 07:58

0 Answers0