views:

338

answers:

3

I have two variables declared as follows:

export portNumber=8888^M
export loginIP_BASE=10.1.172.2^M

I'm trying to print them both on a single line seperated by a colon ':' with the following script. It should look like "10.1.172.2:8888"

echo -n 'Login IP:'
echo -n $loginIP_BASE
echo -n ':'
echo $portNumber

but it it prints out the following:

:8888 IP:10.1.172.2

Why is it doing that and how can I get it to do what I want? Also, the variables are preexisting from another file, so i did not write them myself. What does the "^M" do?

+1  A: 

The file has been transferred from Windows in binary mode, and still has carriage return characters at the ends of the lines.

They are your ^M symbols, and they are causing the text position to return to the start of the line when the values are displayed - the carriage return at the end of the first value makes the second value display at the start of the line again, overwriting part of the first value.

The right fix is to transfer the file from Windows using text mode transfer, or to run dos2unix on the file after you've transferred it. (Or if the file isn't going to be transferred from Windows again, just delete the ^M characters!)

RichieHindle
+1  A: 

In Windows a tipical new line is \r\n (in *nix systems is just \n).

\r is carriage return.

\n is new line.

^M is \r, so after writing $loginIP_BASE you are at position 0 of the actual line.

If you want to remove all those ^M you can do it in vim o with sed using:

sed s/{ctrl+v}{ctrl+m}// file > new_file

({Ctrl+v} means press ctrl and then v)

blaxter