How do I know if a character is a carriage return?

Problem:

I could always scroll characters to identify the char I want. However, now that I want to identify the carriage return, my way of doing something doesn't work:

function removeCarriageReturn()
{
  word=""

  while IFS= read -r -n1 char ; do
    if [ "${char}" != "\r" ] ; then
      word="${word}${char}"
    fi
  done <<<"$1"

  printf '%s\n' "$word"
}

Result:

For some reason, I don't know if "$" is added before carriage returns, why? Here is the result (from Jenkins):

When char is parsed, for example, 8

++ '[' 8 '!=' '\r' ']'

When char is parsed - \ r

++ '[' $'\r' '!=' '\r' ']'
+4
source share
2 answers

Eliminating your real question, the easiest way to remove any carriage values ​​from a value is

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

(which also demonstrates that your operator ifwill read

if [ "$char" != $'\r' ]; then.

Note that this is the same syntax -xused to display carriage returns.)

+4

$'' quoting ANSI-C escape-.

, set -x , . "\r", \r $'\r' .

( set -x) , , . $'\r'.

+5

All Articles