简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM