passing text with \n as one argument in shell

71 Views Asked by At

i have a file called file1 containing the following

firstname lastname location etc
second line
third line

i am using this code

read a b c < file1
echo $a $b $c

the output i am getting is

firstname lastname location etc

so my program is taking

a = firstname 
b = lastname
c= location etc

how can i get my program to take

c= location etc second line third line
2

There are 2 best solutions below

0
On

You can use -d flag in read:

read -d '' a b c < file1

echo "$c"
location etc
second line
third line
0
On

Reading your query you want to have everything in one line.

# read file in one variable
c=`cat file1`
# keep everything in a separate line
echo "$c"
# put everything in one line
echo $c

The last echo will produce the output you have in your question:

firstname lastname location etc second line third line