2

I using shell script to replace a number in the text file . Content of file is:

cat file
7.0.78 | 8.0 | 7.0.47

And I am using below command . This is working from command prompt but not from script.

Actual=8.0.44.0

sed -i -e "s/8.0/$Actual/g" file

I am getting output as | 7.0.478.0.44.0

from debug mode , I noticed that my variable become as:

+ Actual=$'8.0.44.0\r'
+ echo $'8.0.44.0\r'

Please advise, how to fix this issue. Thanks

Hauke Laging
  • 90,279
SuS
  • 39
  • 4
    The \r characters are a tell-tale clue: your script has DOS/Windows line-endings. You need to remove them with dos2unix or similar tool. In the future create scripts using a Unix-aware editor. – John1024 Oct 05 '17 at 06:51
  • Create an answer so that this question can be easily recognized as answered. – Hauke Laging Oct 05 '17 at 08:12
  • @jasonwryan "exact duplicate"? The other question is about an error message in a shell script, this one is about strange behaviour in an sed script. Although the root cause is the same, I vote to reopen, so the comment can be turned into an answer. – Philippos Oct 05 '17 at 08:13
  • I agree with the close decision: the carriage return character is assigned to the shell variable by the shell -- before it's passed as a parameter for the sed command. Given that particular expression, sed isn't behaving strangely at all. – Anthony Geoghegan Oct 05 '17 at 09:37
  • Community means OP accept close reason. – Archemar Oct 05 '17 at 09:42
  • @Sunil It's clearer to see what's actually happened with the sed -i command if you run cat -v file. When printing to screen, the carriage return moves the cursor to the start of the line so that the first 9 characters are over-wriiten. – Anthony Geoghegan Oct 05 '17 at 09:44
  • Thank you so much guys for so many details , I created shell script on one server and then I copied it to other server and issue started . dos2unix didn't helped . After rewriting same script on destination server . I stopped getting this error . – SuS Oct 08 '17 at 00:48

0 Answers0