Bash Concatenating Strings Improperly

bash

Solution

Your file has DOS line endings; the `\r` at the end of `$line` causes the cursor to return to the beginning of the line, which only affects your output when `$line` is not the last thing being printed before the newline. You should remove them with something like `dos2unix`.

You can use something similar to Perl's `chomp` command to remove a trailing carriage return, if one is present:

# $'\r' is bash-only, but easy to type. For POSIX shell, you'll need to find
# someway of entering ASCII character 13; perhaps Control-V Control-M
line=${line%$'\r'}

Useful if, for whatever reason, you can't (or don't want to) fix the input before reading it.

Problem

I have a text file with a list of Mercurial repositories in it, in the form: ``` IDE Install InstallShield ``` I'm writing a bash script to clone/pull/update all the repositories based on the text file. Right now I'm just echoing before I do any actual cloning. If I do: ``` while read line; do echo "hg clone" ${MASTER_HG}/${line}; done < Repos.txt ``` The output is as expected: ``` hg clone /media/fs02/IDE hg clone /media/fs02/Install hg clone /media/fs02/InstallShield ``` However, if I do: ``` while read line; do echo "hg clone" ${MASTER_HG}/${line} ${REPOROOT}/${line}; done < Repos.txt ``` The output is: ``` /var/hg/repos/IDE02/IDE /var/hg/repos/Installnstall /var/hg/repos/InstallShieldShield ``` It seems to be replacing the beginning of the string with the end of the string. Is there some kind of character overflow or something going on? My apologies if this is a dumb question, but I'm a relative noob for bash.

Original source