1

I can't understand this... I have .sh file with variables (varsource.sh)

var1=AppleOrange
var2=Mango

Now I am sourcing my varsource.sh in my test.sh script

#!/bin/ksh
. ./varsource.sh
appended=$var1$var2
echo $appended
varloc1=aPPLEoRANGE
varloc2=mANGO
locappended=$varloc1$varloc2
echo $locappended

The output of above script is

MangoOrange
aPPLEoRANGEmANGO

I expected similar behavior when I use variables from sourced file and variables local to my script. In case of variables from sourced file, the second variable is replacing characters of the first variable instead of appending.

More observations:

  . ./varsource.sh
    appended=${var1}xx
    echo $appended

Output: xxpleOrange

But appending to left end of the variable is working

. ./varsource.sh
appended=xx$var1
echo $appended

Output:xxAppleOrange

Could some one help me understand this behaviour? What should I do to perform the appending in case of sourced variables?

  • 1
    If you need assistance in how to remove the CR, shout. However, there are questions on SO that discuss many ways to do it, such as [How to convert DOS/Windows newline to Unix newline in a Bash script](http://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-to-unix-newline-in-bash-script/). – Jonathan Leffler Jan 04 '14 at 06:04
  • 1
    @Jonathan Thanks it Worked. I replaced CR using **tr -d '\015' UNIX-file** – searchingforPerfection Jan 04 '14 at 08:29

1 Answers1

2

You're on a Windows machine, or the sourced file was created on a Windows machine.

Your line endings are CRLF, carriage return and line feed, DOS/Windows style. The shell treats the carriage return as a regular character, not as part of the 'end of line'.

Remove the carriage returns and all will go back to normal.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278