Reputation: 447
I want an array containing the numbers from 1 to twice the number of lines in a text file. In this example, the file somefile.dat has four lines. I tried:
~$ echo {1..$(( `cat somefile.dat | wc -l`*2 ))}
{1..8}
which is not what i wanted. However, what goes on inside the parenthesis has clearly been interpreted as integers. Why then, is the total result a string? And how do I convert it to an int so that I get the numbers 1 to 8 as output?
Upvotes: 3
Views: 1299
Reputation: 785146
Why then, is the total result a string?
That is because brace range directive in shell {1..10}
doesn't support a variable in it.
As per man bash
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
Examples:
echo {1..8}
1 2 3 4 5 6 7 8
n=8
echo {1..$n}
{1..8}
Alternatively you can use seq
:
seq 1 $n
1
2
3
4
5
6
7
8
Upvotes: 4