Variables overwriting text problem with “echo” in Bash

A wild guess here: you are extracting the IP variable from a .txt file — if that’s a Windows file, or is encoded Windows-style, lines end with \r\n. You take the newline away, but what if there’s a \r in it that’s making you go back to the beginning of the line?

Quick dirty fix with no questions asked: use echo -n, it supresses the newline at the end of the echoed text.

echo -n $IP; echo -n of; echo -n $IPLINES

If the problem persists, it’s probably what I said above. Try right-trimming $IP.

EDIT: didn’t see the OSX part, sorry. In OSX, lines end with \r — that must be the issue.

Leave a Comment