Home > OS >  read a file line by line and assign the values to variable as comma separated
read a file line by line and assign the values to variable as comma separated

Time:01-20

I have the following a.txt file:

abc,
def,
ghi

I want to read it line-by-line, and store in a varibale as comma seperated values

var1=abc,def,ghi

i am new to shell script pls help

My try:

name="file.txt"
while IFS=read -r line
do
    names=`echo $line`
done < "name"

it is displaying only value ghi to varibale

CodePudding user response:

You're not concatenating, you're replacing the names variable each time through the loop.

There's no need to use echo when assigning the variable.

name="file.txt"
names=
while IFS=read -r line
do
    names="$names$line"
done < "name"
  • Related