Creating variable by for loop

i have a file 'detail' which contains

 cat detail
111111
222222
333333
444444

but detail may be 4 line file.6 line file or 8 line file like

cat detail
111111
222222
333333
444444
555555
666666
777777
888888

so i want a declare a loop which assign the value of first line in one variable and second to second variable and so on as per the file whatever it is 4 , 6 or 8.
please help.

thanx

What exactly do you want to with this file?
You want to print those lines or give them as input?

i want to use those variable as an input to further part...............

you can use while loop if you want to input all to some script...

while read var
do
  thing-to-be-done
done < file-name

Or if you just want to print them :

Use :

sed -n <line to be printed>p file-name

thanx for ur reply but i hv tried this................

export line1=`sed -n '1p' details.txt`
export line2=`sed -n '2p' details.txt`
export line3=`sed -n '3p' details.txt`
export line4=`sed -n '4p' details.txt`

but i want to do by loop to assign the variable......can u please help me out how to write
exact code for this?

Well, what's your system, what's your shell?

In BASH you can do this:

C=0
while read LINE
do
        read "VAR${C}" <<<$"LINE"
        ((C++))
done <file

which will set VAR0, VAR1, ...

---------- Post updated at 01:11 PM ---------- Previous update was at 11:55 AM ----------

Actually, this will work in any shell:

C=0

while read LINE${C}
do
        C=`expr $C + 1`
done < file

i used first code which giving an error

 `<' is not expected.

in this line

read "VAR${C}" <<<$"LINE"

Obviously you don't have BASH, then. What is your shell?

I made a typo in my post too, should be "$VAR" not $"VAR".

Did you try my second example? That's more likely to work in a non-BASH shell.

Try this altered Corona's code

C=0
while read LINE
do
        eval "VAR${C}"=$LINE
        ((C++))
done <file

echo $VAR0
echo $VAR1
...

--ahamed

That's a dangerous and pointless waste of an eval -- anything in backticks read from that file will be executed! -- since my second example ought to work just fine without the eval.

May I ask why? I didn't get your point. Please enlighten me.

--ahamed

Eval evaluates all shell code it's given, including variables and backticks. If some joker adds `rm -Rf ${HOME}` into that file, eval will run that code.

It's also pointless because read takes variable names in the first place -- you don't need eval to change the name, ordinary substitution will do.