Search code examples
bashstring-substitution

bash native string manipulation displays jumbled output


I tried to use the bash string native manipulation for substituting string with my shell variables.

var1='123'
var2='2018-01-01'
var3='2018-01-02'
var4='myfunction('var1','var2','var3')'

var5=${var4/var1/$var1}
echo $var5

var5=${var5/var2/$var2}
echo $var5

var5=${var5/var1/$var3}
echo $var5

Expected output:

myfunction('123','var2','var3')
myfunction('123','2018-01-01','var3')
myfunction('123','2018-01-01','2018-01-02')

Actual output with jumbled strings:

myfunction('123','var2','var3')
myfunction('123','2018-01-01','var3')
')function('123','2018-01-01','2018-01-02

Here the last two characters shift at the beginning and I lose the first two characters of the string. I can use SED for the same. But I am just trying to figure out why will the bash native string manipulation not work as expected. Is it because I am doing multiple substitutions ?

Thanks for your help.


Solution

  • I was able to solve it by using dos2unix command on the file. The error was due to carriage return (CR) character at the end of var3 content.