bash removes consecutive spaces from string [duplicate] - string

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"

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"

How to write result in empty variable? [duplicate]

This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Closed 2 years ago.
How to write command result into empty variable?
#!/bin/bash
today=''
$today = $(date)
echo $today
There shouldn't be a space around the =
On variable assignment, no need for the $
#!/bin/bash
today=''
today="$(date)"
echo "${today}"

Output exactly x number of characters to variable using read in Bash [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop

Bash how to store in a variable the result of a linux command? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to store in a variable the temperature of the computer. I tried this but it doesn't work:
#!/bin/bash
temp = cat "/sys/class/thermal/thermal_zone0/temp"
echo "$temp"
i tried this too:
#!/bin/bash
temp = $(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"
but nothing works, it always says
./temp.sh: line 2: temp: command not found
Spaces are crucial! This works fine:
# NO space around `=`
temp=$(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"

Why this shell script doesn't work? [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 9 years ago.
n=5
for i in {1..$[n]}
do
echo $i
done
it gives:
{1..5}
But I think it should output:
1
2
3
4
5
Why it gives such a strange output?
That is almost a riddle. The expansion of the braces is being done prior to the variable expansion. The bash beginners guide has some good detail on expansion
There are a brazillion ways to do this in bash.
You could start with:
n=5
for i in $(eval echo {1..$n})
do
echo $i
done

Resources