When bash displays strings, it interprets the carriage return '^M'
$ echo "1234^Mab"
ab34
I have a script that needs access to the interpreted string "ab34". But I can't find any way to do this. The output stream of the echo command still contains the carriage return character, which means that if a script reads the output, it will still see "1234^Mab", even though the shell displays the string "ab34".
Removing the carriage return is not good enough; It does not result in the string "ab34".
$ echo "1234^Mab" | tr -d '\r'
1234ab
Converting the carriage return to a unix-style newline character doesn't produce the desired result either:
$ echo "1234^Mab" | sed 's/^M/\n/'
1234
ab
Use the col -b
command to interpret cursor control sequences and return the visible output.
echo $'1234\rab' | col -b
This handles about a dozen different cursor motion sequences, not just carriage return.