Difference between single quotes and double quotes [duplicate] - linux

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.

Related

How do I use a variable with special characters in bash? [duplicate]

This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Closed 1 year ago.
I have this variable in my bash script:
var="\'?"\"'\"
but when I type echo $var I get nothing. I want echo $var to return "\'?"\"'\". What should I do here?
You can try this
var="'''?\"\"\""
or
var='```?"""'
echo "$var"

bash removes consecutive spaces from string [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 2 years ago.
I ran into a strange problem that I do not understand. Why are multiple spaces not present in the output of the following command?
$ d='A B'
$ echo $d
A B
use in double quotes:
echo "$d"

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

Shell: insert variable into a command [duplicate]

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)

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