Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove carriage return from a variable in shell script

Tags:

shell

unix

I am new to shell script. I am sourcing a file, which is created in Windows and has carriage returns, using the source command. After I source when I append some characters to it, it always comes to the start of the line.

test.dat (which has carriage return at end):

testVar=value123 

testScript.sh (sources above file):

source test.dat echo $testVar got it 

The output I get is

got it23 

How can I remove the '\r' from the variable?

like image 813
Lolly Avatar asked Mar 20 '13 09:03

Lolly


People also ask

How do I remove a carriage return in vi?

vi. You can even remove carriage return (Ctrl+M) characters with vi, although this assumes you're not running through hundreds of files and are maybe making some other changes, as well.


1 Answers

yet another solution uses tr:

echo $testVar | tr -d '\r' cat myscript | tr -d '\r' 

the option -d stands for delete.

like image 121
Nik O'Lai Avatar answered Sep 21 '22 03:09

Nik O'Lai