In BASH, How do i replace \r from a variable that exist in a file written using HTML <textarea></textarea>

30.4k views Asked by At

How do i replace the \r?

#!/bin/bash
...

# setup
if [[ $i =~ $screen ]]; then

    ORIGINAL=${BASH_REMATCH[1]}          # original value is: 3DROTATE\r
    AFTER   =${ORIGINAL/\\r/}            # does not replace \r
    myThirdPartyApplication -o $replvar  # FAILS because of \r

fi
5

There are 5 answers

2
imm On BEST ANSWER

You could use sed, i.e.,

AFTER=`echo $ORIGINAL | sed 's/\\r//g'`
0
Neil On

Just use a literal ^M character, it has no meaning to bash.

2
tharrrk On

This should remove the first \r.

AFTER="${ORIGINAL/$'\r'/}"

If you need to remove all of them use ${ORIGINAL//$'\r'/}

0
3molo On

Another option is to use 'tr' to delete the character, or replace it with \n or anything else.

 ORIGINAL=$(echo ${BASH_REMATCH[1]} | tr -d '\r')
0
user2510797 On

Similar to @tharrrk's approach, this parameter substitution also remove the last '\r':

AFTER="${ORIGINAL%'\r'}"