Shell: insert variable into a command [duplicate] - linux

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I'm trying to find a way the use a variable in this command to replace -10 with n_days var:
n_days= -10
date_prefix=$(date -d '-10 day' +%Y/%m/%d)
I tried this way but it didn't work:
date_prefix=$(date -d '${n_days} day' +%Y/%m/%d)

Two things:
Declare your variable properly (there is a space in your example)
Use double quotes in place of single quotes to allow the variable to be interpolated
So:
n_days=-10
date_prefix=$(date -d "$n_days day" +%Y/%m/%d)

Related

Why set variable with output of bash command use only first line? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I want to use:
schema=$(kubectl exec -n $namespace -it $podName -- bash -c "./spiral orm:schema")
echo $schema
But eventually in schema variable recorded only the first line from the result of bash execution.
How to make it use all lines?

How to add a variable to matched expression in sed? [duplicate]

This question already has answers here:
How to find/replace and increment a matched number with sed/awk?
(5 answers)
Closed 4 years ago.
Suppose I have the following Bash script:
#!/bin/bash
INCREMENT_BY=5
sed 's/20000/&+$INCREMENT_BY/g' old > new
I expect all occurrences of 20000 to be replaced by 20005, but instead they are replaced with 20000+$INCREMENT_BY. How can I make this work?
You should use double quote for eval variable, like:
sed "s/20000/$(( $INCREMENT_BY + 2000))/g" old

Difference between single quotes and double quotes [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
bash string with/without quote and single/double quote [duplicate]
(1 answer)
Closed 5 years ago.
I have two variables declared in shell script
var1=23
var2=27
echo $var1 # Prints 23
echo '$var1' # Prints $var1
echo "$var1" # Prints 23
From the above I have some confusion regarding single and double quotes usage on variables.
please can any one clarify this with an example difference between single and double quote.

Replace a text with a variable [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 7 years ago.
How can I do this?
sed -i 's/wiki_host/$host_name/g' /root/bin/sync
It will replace wiki_host with the text $host_name.
But I want to replace it with the content of the variable..
I tried it with
sed -i 's/wiki_host/${host_name}/g' /root/bin/sync
It doesn't work either.
You need to use double quotes:
$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync
Your single quotes prevent the shell variable from being replaced with its contents.

How to store and echo multiple lines elegantly in bash? [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 5 years ago.
I'm trying to capture a block of text into a variable, with newlines maintained, then echo it.
However, the newlines don't seemed to be maintained when I am either capturing the text or displaying it.
Any ideas regarding how I can accomplish this?
Example:
#!/bin/bash
read -d '' my_var <<"BLOCK"
this
is
a
test
BLOCK
echo $my_var
Output:
this is a test
Desired output:
this
is
a
test
You need to add " quotes around your variable.
echo "$my_var"

Resources