简体   繁体   中英

Bash replaceing string i don't want it to

I have a list that I want to run a command on so I was testing it first with an echo to make sure it was correct but it printed it out wrong.

while read name; do echo $name; done < RandomNames

This prints out just this list exactly how I want it but if I put anything after the variable it replaces the start of it.

So if I write

while read name; do echo $name; done < RandomNames

It will print

Rich
Chris 
Zack

but if I write

while read name; do echo $name t; done < RandomNames

it writes

 tch
 tris
 tck

so it replaces the first two characters with what ever I put after the variable and I have no idea why.

Your file has DOS newlines, so each input line ends with a hidden $'\\r' character that moves the cursor to the beginning of the line after that line is printed, such that the next character written overwrites the line's first character.

Either use dos2unix or a similar tool to convert them to UNIX newlines, or expand ${name%$'\\r'} to trim them.

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