-1

I do this:

VAL=$(wc -l < file_with_5_lines)
for i in {1..${VAL}}; do echo $i; done

Expecting this result:

1
2
3
4
5

Instead of that I get this one:

{1..5}

EDIT

This question was marked as duplicate but the accepted answer for the other question isn't valid in my opinion. The proposed solution is this:

VAL=$(wc -l < file_with_5_lines)

for i in {1..$((VAL))}; do
        echo $i
done

And continues to give me this result:

{1..5}

Instead of:

1
2
3
4
5
Sergio
  • 844
  • 2
  • 9
  • 26

3 Answers3

2

Brace expansion is done before parameter expansion and that's why we can't have a variable inside {...} construct. Use a regular for loop so that you don't depend on an external command like seq:

for ((i = 1; i <= VAL; i++)); do
    # your code here
done
codeforester
  • 39,467
  • 16
  • 112
  • 140
1

Try the below code,

VAL=$(wc -l < file_with_5_lines)
for i in `seq ${VAL}`
do 
  echo $i
done
sprabhakaran
  • 1,615
  • 5
  • 20
  • 36
0
VAL=$(wc -l < file_with_5_lines)
for i in $(seq $VAL);do echo $i;done
gboffi
  • 22,939
  • 8
  • 54
  • 85
Buddhi
  • 416
  • 4
  • 14