Prevent variable expansion in Bash heredocument [duplicate] - bash

This question already has answers here:
How to cat <<EOF >> a file containing code?
(5 answers)
Using variables inside a bash heredoc
(3 answers)
Escaping a dollar sign in Unix inside the cat command
(2 answers)
How to split strings over multiple lines in Bash?
(12 answers)
Closed 4 months ago.
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!

Related

What does the #*$ in a shell script's string interpolation do? [duplicate]

This question already has answers here:
What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
(4 answers)
Closed 11 months ago.
I found the following code in a shell script, but I am unsure of what the test condition is evaluating for
if test "${SOME_VAR#*$from asdf/qwer}" != "$SOME_VAR"; then
echo "##zxcv[message text='some text.' status='NORMAL']";
fi
The combination #*$ does not mean anything special. There are three special symbols and each of them has its own meaning in this context.
${SOME_VAR#abc} is a parameter expansion. Its value is the value of $SOME_VAR with the shortest prefix that matches abc removed.
In your example, abc is *${from} asdf/qwer. That means anything * followed by the value of variable $from (which is dynamic and replaced when the expression is evaluated), followed by a space and followed by asdf/qwer.
All in all, if the value of $SOME_VAR starts with a string that ends in ${from} asdf/qwer then everything before and including asdf/qwer is removed and the resulting value is passed as the first argument to test.
Type man bash in your terminal to read the documentation of bash or read it online.

How to encase a command line argument in quotes with a bash/shell script [duplicate]

This question already has answers here:
How do I pass in the asterisk character '*' in bash as arguments to my C program?
(5 answers)
The issue of * in Command line argument
(6 answers)
Closed 5 years ago.
How do I write a bash script/function that will take a command line argument with spaces and interpret it as if it had quotes around it.
ie: echo this is some text as if echo "this is some text"
What I want to do is create a simple CLI calculator script/function.
Here is what I have:
calc() {
echo $(($#))
}
On the CLI, all of these work:
"# +-/* ", #+-/*, # +-/ #
ie:
calc 10+2
12
but this one produces an error:
calc 10 * 2
-bash: 10 calc.sh 2: syntax error: invalid arithmetic operator (error token is ".sh 2")
Any ideas? It's not a big deal to include the quotes in the calculations, but if possible it would be quicker/more convenient to not include them. Is there any way to take a string after calc from first character to last and then pass it through quotes in the script?
In unix systems there is a variable called IFS. This variable decides at what signs the console separate the string into words. In theory you could chang the IFS variable, but this is really not advisable (as it will breake other bash commands).
Better: you could write your utillity in such way, that writing 'calc' will prompt the user to type his equation to standard in. Then you can read the text the user types and parse it in any way you want.
Even better: a shell script can specify that it wishes for an arbitrary nimber of words as input. Take all the words the user writes and then parse that. See: How to define a shell script with variable number of arguments?

UNIX Replace string without replacing space [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)
Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.

Echoing an environment variable, keeping newlines intact? [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 7 years ago.
I want to create some scripts for filling some templates and inserting them into my project folder. I want to use a shell script for this, and the templates are very small so I want to embed them in the shell script. The problem is that echo seems to ignore the line breaks in my string. Either that, or the string doesn't contain line breaks to begin with. Here is an example:
MY_STRING="
Hello, world! This
Is
A
Multi lined
String."
echo -e $MY_STRING
This outputs:
Hello, world! This Is A Multi lined String.
I'm assuming echo is the culprit here. How can I get it to acknowledge the line breaks?
You need double quotes around the variable interpolation.
echo -e "$MY_STRING"
This is an all-too common error. You should get into the habit of always quoting strings, unless you specifically need to split into whitespace-separated tokens or have wildcards expanded.
So to be explicit, the shell will normalize whitespace when it parses your command line. You can see this if you write a simple C program which prints out its argv array.
argv[0]='Hello,'
argv[1]='world!'
argv[2]='This'
argv[3]='Is'
argv[4]='A'
argv[5]='Multi'
argv[6]='lined'
argv[7]='String.'
By contrast, with quoting, the whole string is in argv[0], newlines and all.
For what it's worth, also consider here documents (with cat, not echo):
cat <<"HERE"
foo
Bar
HERE
You can also interpolate a variable in a here document.
cat <<HERE
$MY_STRING
HERE
... although in this particular case, it's hardly what you want.
echo is so nineties. The new (POSIX) kid on the block is printf.
printf '%s\n' "$MY_STRING"
No -e or SYSV vs BSD echo madness and full control over what gets printed where and how wide, escape sequences like in C. Everybody please start using printf now and never look back.
Try this :
echo "$MY_STRING"

bash script whole file reading into a variable with newline [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 years ago.
i want to read whole file in a variable.
for example my file is.
file name : Q.txt
My name is Naeem Rehmat.
I am a student of FAST university.
Now i am in 4th semester.
I am learning bash script.
I have this problem.
code:
text=$(cat Q.txt)
echo $text
out put should be like this:
My name is Naeem Rehmat.
I am a student of FAST university.
Now i am in 4th semester.
I am learning bash script.
I have this problem.
Presumably the problem is that your whitespace is incorrect. Use double quotes:
echo "$text"
When you write echo $text without quotes, bash evaluates the string and performs what is known as "field splitting" or "word splitting" before generating the command. To simplify the case, suppose text is the string foo bar. Bash splits that into two "words" and passes "foo" as the first argument to echo, and "bar" as the second. If you use quotes, bash passes only one argument to echo, and that argument contains multiple spaces which echo will print.
Note that it is probably good style to also use quotes in the assignment of text (ie, text="$(cat Q.txt)"), although it is not necessary since field splitting does not occur in a variable assignment.

Resources