How to use variable in head function in loop in unix shell

I am very new and I am working on variables.

I wonder how I can use a variable in the head function

eg,

for ((i=0; i<5; i++)) head -((1+i)) test.txt 

but it displays an error. how can i fix this?

+4
source share
1 answer

Something like that:

 for ((i=1; i<6; i++)) do head -n$i test.txt done 

Please note that I ran the variable i in 1 so that we would not get an error from head (for example: head: illegal line count -- 0 ). And you can access the variable i using $i in your loop.

+2
source

All Articles