I have this script where I’m reading a text file line by line and executing a command. I found that the strings (aka lines) always ends with '\r'.
I wanna remove the CR from the end of the string.
This is how my code looks like:
file="myfilelist.txt"
while IFS= read -r filename
do
git log --oneline -- ${filename} |wc -l
done <"$file"
I wanna be able to perform the command in the loop without \r at the end of every line string (aka filename).
Try this:
Note that you don’t need the curly braces when
$filenameis surrounded by whitespace.In principle you can also do this via bash’s own parameter expansion mechanism:
but the
^Mhas to be a literal control-M character, which is ugly and difficult to maintain.Or you can delete all whitespace characters at the end of the name (which includes
^Mas well as space, tab, et al):But now we’re getting into techniques that very few people are likely to recognize without reading the manual.