Bash Shell – Error with String Manipulation – Tech Tips

Bugs and Bugs and that is what we live with

I was running a simple bash script and somehow the output was all wrong. I debugged the script and looked it multiple times with lot of printf but still could not find the issue..

Bug was: When assigning a variable, i was leaving a space between = and the string. Look at my series of bugs and please do not repeat my mistakes.

var= “do something” + ” and do something” # this is wrong not needed +

var= “do something””var2 shall do something” # this is wrong as var2 has to be $var2

var = “do something” # bash doesnt like u putting SPACE after = and it messes things up

var= $var2″ and some more $val3/$val4″ # to output another var use $ and no space needed

echo $var

Syntax:

VARNAME=VALUE

IMP: Bash doesnt take kindly when we put a space around”=” sign in variable assignment. I hate it and especially we who have grown out of .Net who is all too forgiving.

IMP2: When you use VAR = VALUE, the shell assumes that VAR must be the name of a command and tries to execute it.

 

Bash Shell String Bug tech tips from cognosys