Victor
Victor

Reputation: 5807

echo printing variables on single line

I have two variables:

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

I'm trying to print them both on a single line separated by a colon ':'. 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 this instead:

:8888 IP:10.1.172.2

Why is it doing that? 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?

Upvotes: 0

Views: 6374

Answers (3)

Gvtha
Gvtha

Reputation: 1503

Use dos2unix command:

dos2unix filename

One more trick to remove Ctrl+M in vi editor:

:%s/^V^M//g

Upvotes: 0

RichieHindle
RichieHindle

Reputation: 281405

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!)

Upvotes: 1

blaxter
blaxter

Reputation: 335

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)

Upvotes: 2

Related Questions