How to read a whole line from bash

I have a file file.txtwith contents like

i love this world

I hate stupid managers
I love linux

I have MS

When I do the following:

for line in `cat file.txt`; do
echo $line
done

It gives a conclusion, for example

I
love
this
world
I
..
..

But I need the output as whole lines, such as below - any thoughts?

i love this world

I hate stupid managers
I love linux

I have MS
+5
source share
5 answers
while read -r line; do echo "$line"; done < file.txt
+10
source

As @Zac notes in the comments, the simplest solution to the question you post is simple cat file.txt, so I have to assume that there is something more interesting, so I put two options that solve the question, how was it asked

: IFS (Internal Field Separator) , read line while

IFS="
"

(while read line ; do
    //do something
 done) < file.txt
+6

, read, stdin. script, stdin , stdin.

#!/bin/bash
file=$1 

# save stdin to usually unused file descriptor 3
exec 3<&0

# connect the file to stdin
exec 0<"$file"

# read from stdin 
while read -r line
do
    echo "[$line]"
done

# when done, restore stdin
exec 0<&3
+1

, , . script . "", $REPLY.

cat file.txt | ; do echo $REPLY;

Dave..

+1

Try

(while read l; do echo $l; done) < temp.txt

: .

Reads a single line from standard input or from a file with the FD descriptor if the -u option is enabled. The line is divided into fields, as with the word division, and the first word is assigned to the first NAME, the second is the word for the second NAME, etc., with any other words assigned to the last name. Only characters found in $ IFS are recognized as word delimiters.

-1
source

All Articles