jshen
jshen

Reputation: 72

bash echo multiple variables that overrides each other

I'm trying to print two variables in the same echo command in bash, the first variable "ID" is obtained by looking at the first line of a .c file. I strip the prefix to obtain ID = myid.

first line of .c file

// EID:myid

strip the prefix

firstLine=$(head -n 1 ~/my.c)
ID=${firstLine#*:}

if I echo $ID, I get "myid"; however, if I echo something along the line of:

randomString="random"
echo $ID$randomString

randomString will completely override my ID, and it'll only display

random

What am I missing about stripping prefix in bash? Thanks for your time!

Upvotes: 1

Views: 267

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

Your .c file uses MS-DOS line endings and therefore $ID has a CR at the end. Strip that as well.

Upvotes: 5

Related Questions