i am writing a script in bash and I would like to give the two numbers in the brackets, x and y to be precise, as variables.
I wrote the script this way:
echo "x?"
read x
echo "y"
read y
for i in {$x..$y}
but it does not work.
How could I set it up to make it function?
Thanks a lot in advance.
Best Answer
Use this simple
bash
solution:Before using the following functions, read below !
For a sequence of numbers, you might use
seq
:If you really need brace expansion, you can use
eval
:Disclaimer:
eval
is evil. This case is a perfect example: If the user writes$(do_something_evil)
instead of a number,do_something_evil
will be executed. So before usingeval
, make sure your input is not evil. The same applies toseq $x $y
.If you still want to use one of these functions, test the user input, which is generally a good idea: How do I test if a variable is a number in Bash?