Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash programmation (Cygwin): Illegal Character ^M [duplicate]

I have a problem with a character. I think it's a conversion problem between dos and unix.

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

0.495959

But when I try to make an operation on that value with the bc command (I am 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 on my .sh file. I think it's because my variable have the ^M character (not printed with the echo command)

How can i eliminate this error?

like image 272
Frencoo Avatar asked Nov 29 '11 19:11

Frencoo


People also ask

Is Cygwin a bash?

The Cygwin installation creates a Bash shell along with a UNIX environment by which you can compile and run UNIX-like programs. Using this one can even create an emulated X server on your windows box and run UNIX-like GUI software tools.

What is ${ 2 in bash?

$2 is the second command-line argument passed to the shell script or function.


1 Answers

I don't have Cygwin handy, but in regular Bash, you can use the tr -d command to strip out specified characters, and you can use the $'...' notation to specify weird characters in a command-line argument (it's like a normal single-quoted string, except that it supports C/Java/Perl/etc.-like escape sequences). So, this:

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

will strip out carriage-returns on the way from echo to bc.

You might actually want to run this:

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

which will modify $mean to strip out any carriage-returns inside, and then you won't have to worry about it in later commands that use it.

(Though it's also worth taking a look at the code that sets $mean to begin with. How does $mean end up having a carriage-return in it, anyway? Maybe you can fix that.)

like image 97
ruakh Avatar answered Oct 02 '22 02:10

ruakh