I have a bash script which reads lineas
filename=$
while read LINE
do
......
done < $filename
I wonder how could I store $LINE in a string (my_string) so for each line I can do
echo $my_thing" "$my_string
I tried several things but when I print with echo or printf, $LINE deletes everything before it
Tjans
Your file may have carriage returns in it. For example, it may be a DOS or Windows file that has that style of line endings. If that is the case, you can run dos2unix
on the file to convert it to Unix-style line endings or you can strip them as you read them:
LINE="${LINE//$'\r'}"
which would go right after the do
statement.
when you do a while read loop like that, the line is stored in variable $LINE, so try this
while read -r LINE
do
echo "$LINE $LINE"
done <"file"
if you want to store LINE in another string, do this
my_other_string=$LINE
I think Dennis Williamson identified why you're seeing the "deletes everything printed before it" behavior.
Let me just point out that if you're going to do "while read ..." processing in a bash script, you'll want to use both the -r flag to read and set your IFS to the null string. The first is needed to keep any backslashes in the stdin handled raw, and the second is needed to avoid trimming whitespace at the start and end of the stdin. You can combine them like this:
while IFS= read-r LINE; do
echo "$LINE"
done < "file"
精彩评论