Bash (Cygwin): Illegal Character ^ M

I have a problem with a character. I think this is a conversion problem between dos and unix.

I have a variable that is a float value. When I print it using the echo command, I get:

0.495959

But when I try to perform an operation on this value using the bc command (I'm not sure how to write the bc command).

echo $mean *1000 |bc

I get:

(standard_in) 1 : illegal character: ^M

I already use the dos2unix command in my .sh file. I think because my variable has the ^ M character (not printed with echo command)

How can I fix this error?

+5
source share
7 answers

Cygwin, Bash tr -d , $'...' ( , , C/Java/Perl/ ..). , :

echo "$mean" * 1000 | tr -d $'\r' | bc

echo bc.

, :

mean=$(echo "$mean" | tr -d $'\r')

$mean, , , .

( , $mean . $mean , ? , .)

+11

:

${mean/^M/}

^ M, Ctrl-V, Ctrl-M. , :

${mean/$(printf "\r")/}

@ruakh , bash . , .

"unixize" $mean:

mean="${mean/^M/}"

: :

${mean/$'\r'/}

+2

, script UNIX DOS.

+1

Windows- cygwin , - Windows cygwin bash CR .

d2u - ,

runtime="`mediainfo --Inform='Video;%Duration%' ${movie} | d2u`"

( d2u, ${runtime} CR, , , , "bc", .)

+1

:

echo `echo $mean` *1000 |bc

, .

0

^M , Windows (\n) . , , UNIX, bash , . , , . dos2unix , .

0

, Windows. , , .

:

  • WINDOWS, ​​ Cygwin. CRLF . , , , .

  • Windows, Winpad.

. . VI, VIM, Windows Cygwin ( Unix). VIM , Notepad ++. Unix Windows Windows Cygwin.


  • VIM, , :

    • , , :set ff? .
    • Unix, :set ff=unix .
    • Windows, :set ff=dos .
  • Notepad ++

    • Edit → EOL Conversion , ( ) .
    • Notepad ++ Unix , " → ". " / ". "", " ", . : Mac . Mac. Mac, Unix.
0

All Articles