How to remove carriage return from a variable in shell script

ShellUnix

Shell Problem Overview


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?

Shell Solutions


Solution 1 - Shell

yet another solution uses tr:

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

the option -d stands for delete.

Solution 2 - Shell

You can use sed as follows:

MY_NEW_VAR=$(echo $testVar | sed -e 's/\r//g')
echo ${MY_NEW_VAR} got it

By the way, try to do a dos2unix on your data file.

Solution 3 - Shell

Because the file you source ends lines with carriage returns, the contents of $testVar are likely to look like this:

$ printf '%q\n' "$testVar"
$'value123\r'

(The first line's $ is the shell prompt; the second line's $ is from the %q formatting string, indicating $'' quoting.)

To get rid of the carriage return, you can use shell parameter expansion and ANSI-C quoting (requires Bash):

testVar=${testVar//$'\r'}

Which should result in

$ printf '%q\n' "$testVar"
value123

Solution 4 - Shell

use this command on your script file after copying it to Linux/Unix

perl -pi -e 's/\r//' scriptfilename

Solution 5 - Shell

Pipe to sed -e 's/[\r\n]//g' to remove both Carriage Returns (\r) and Line Feeds (\n) from each text line.

Solution 6 - Shell

for a pure shell solution without calling external program:

NL=$'\n'    # define a variable to reference 'newline'

testVar=${testVar%$NL}    # removes trailing 'NL' from string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLollyView Question on Stackoverflow
Solution 1 - ShellNik O'LaiView Answer on Stackoverflow
Solution 2 - ShellXavier S.View Answer on Stackoverflow
Solution 3 - ShellBenjamin W.View Answer on Stackoverflow
Solution 4 - ShellVorsprungView Answer on Stackoverflow
Solution 5 - ShellmcandreView Answer on Stackoverflow
Solution 6 - ShellThe O.G. XView Answer on Stackoverflow