Unexpected Echo Behavior

Hello all,

I have a basic issue that I can't seem to search for since I'm not sure how to describe the behavior. Could anyone kindly assist the novice?

(Sample) File Data:

bundle-ppp-1/1.78
bundle-ppp-1/2.80
bundle-ppp-1/1.79
bundle-ppp-1/2.81
bundle-ppp-1/1.80
bundle-ppp-1/2.82
bundle-ppp-1/1.81
bundle-ppp-1/2.83
bundle-ppp-1/1.82
bundle-ppp-1/2.84

Code:

# for all in `cat output`; do echo "/configure port $all shutdown"; done

Expected output:

/configure port bundle-ppp-1/1.78 shutdown

Actual output:

shutdowne port bundle-ppp-1/1.78

I see that it has taken the "shutdown" portion of the echo command and overwritten the word "configure" but I don't understand why or how to correct the behavior.

Thanks for taking the time to read my post :slight_smile:

Looks like there are carriage returns in your text file, i.e. it's a DOS/Windows text file.

Regards,
Alister

1 Like

Thank you for the reply. I feel quite silly for not realizing this. I just reformatted the carriage returns and everything works properly.

After you've been bitten by this once or twice, you'll always suspect it when you see overwriting at the start of a display line.

Regards,
Alister

1 Like

Also: That's a useless use of cat and a useless use of backticks.

If your lines had spaces in them, this code will break down.

If the file is too large, this code will break down.

Much better done with a while-read loop.

while read LINE
do
...
done < inputfile
1 Like

Thanks for the articles Cor, I'm new and looking to improve. I'd be embarrassed to post some of the spaghetti code I string together to get my tasks done. As I keep learning, I'm striving for simple, efficient code. I enjoy this world :slight_smile:

Another way using exec:

exec < infile

while read LINE
do
    ....
done