Does bash recognize a variable declaration as string if you do not put the quotation marks?

name=Jackson
echo $name

This will certainly present Jackson.

name="Jackson"
echo $name

This will certainly additionally present Jackson.

So I think by placing quote marks around a variable it is making it a string. What would certainly call = Jackson practically be? Would certainly it additionally be identified as a string?

8
2022-07-25 20:40:13
Source Share
Answers: 1

there is a difference: try

unset name
name="Michael Jackson" ; echo "$name" 

and

unset name #to reset name, see comments
name=Michael Jackson ; echo "$name"

It's very different: on the 2nd, it tries to start the "Jackson" command, passing it the "name=Michael" environment. So it will complain that "Jackson: command not found" I have to add the unset name to "reset" name, as the next line's name=Michael part will be only passed to the "Jackson" command (and only to that command), and is therefore NOT done on the calling shell level! (which therefore would still have the name="Michael Jackson" from the 1st command, if we didn't set it back to "" before our 2nd try. So the "echo $name" would confusingly still display "Michael Jackson" if we didn't forget that 1st try.)

In usual, always put surrounding double-quotes when you define, and especially when you use variables.

cd "${var}" 

is much better than

cd $var 

The 2nd version would silently go to the homedir instead of protesting if $var is not defined...

Then only take out the surrounding double-quotes only when you know that you need to separate each elements of the variable, ie in a :

for i in $var ; do something_using_i ; done
1
2022-07-25 21:54:15
Source