Print a variable containing spaces in bash [duplicate] - bash

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I have this string:
string="I love spaces"
I would like to print that so that the spaces would remain. echo $string doesn't seem to print the spaces as well.
Desired Output:
I love spaces

Nevermind me fellas. echo "$string" :/

Related

Move character in to a new line in bash [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 4 months ago.
I have a variable 'p' which has values 'first second third'
echo $p
first second third
I want to make this output as below
echo $p
first
second
third

Bash initialize variable to a string that contains quotes and backslahes [duplicate]

This question already has answers here:
Setting an argument with bash [duplicate]
(2 answers)
Closed 5 months ago.
I have a string containing quotes and backslahes:
-options \'{"version": "http"}\'
I would like to initialize a variable PARAM with this string.
How this can be done in bash?
I thought of adding it to an array: PARAMS=(-options \'{"version": "http"}\')
but the output I am getting is: -options '{version: http}' i.e. without the slashes.
Expected output: -options \'{"version": "http"}\'
Can someone please suggest?
This looks ok to me.
test="-client-options \\'{\"quic-version\": \"h3\"}\\'"
echo "$test"
t2=("$test" "etc")
echo ${t2[#]}
Escape every inner " and double escape for a persisting escape

shell script count consecutive spaces [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've got a string h="aaaa bbb" which contains 2 spaces between aaaa and bbb,and I want to count the number of spaces in it.However,when I try
echo $h|grep -o ' '|wc -l
it shows 1 instead of the desired two.
Is there any way not to treat consecutive spaces in string as one?

How to simply combine field separated by a slash [duplicate]

This question already has answers here:
How can I join elements of a Bash array into a delimited string?
(34 answers)
Closed 7 years ago.
How to simply combine field separated by a slash ?
LIST=("a" "b" "c")
STRING=???
echo $STRING
a/b/c
Please someone help? Thank you.
In BASH you can do:
list=("a" "b" "c")
printf -v str "%s/" "${list[#]}"
str="${str%/}"
Check output:
echo "$str"
a/b/c
Avoid all CAPS variables in BASH.
Alternatively using IFS:
str=$(IFS=/; echo "${list[*]}")

Why are white spaces in a string trimmed in shell when it is assigned to a variable? [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'm assigning a string (my name is xxxxxx ssddd) a shell variable(str)
when echo the variable.
echo $str
my name is xxxxxx ssddd
Why white spaces are not taken in to account?
Quote your strings in order to preserve white space:
a="four spaces"
echo $a
four spaces
echo "$a"
four spaces
However, when assigning one variable to another, whitespace also is preserved:
a=$b
echo "$b"
four spaces

Resources