Forum Moderators: bakedjake

Message Too Old, No Replies

String variables in shell script

         

SlowMove

10:30 pm on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know how to assign each line of a text file to a string variable in a bash script?
I tried the following:


for x in $(cat ./filename)
do
echo $x
done

What happened is that it assigned each word instead of each line in the file to the variable.

SeanW

1:03 am on Jan 19, 2004 (gmt 0)

10+ Year Member



I think it's a limitation of for... Try "read" instead

cat filename ¦ while read A; echo $A; done

Sean

fpmurphy

1:37 am on Jan 19, 2004 (gmt 0)

10+ Year Member



Another way ...

while read A
echo $A
done < filename

- Finnbarr

hfaridi

6:01 pm on Feb 3, 2004 (gmt 0)



You can also do this:

cat filename ¦ while read line; do $line; done