Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in bash seq replacement ({1..10}) [duplicate]

Tags:

bash

seq

Is it possible to do something like this:

start=1 end=10 echo {$start..$end} # Ouput: {1..10} # Expected: 1 2 3 ... 10 (echo {1..10}) 
like image 707
Tyilo Avatar asked May 31 '11 17:05

Tyilo


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is variable substitution Linux?

Example. Command substitution is generally used to assign the output of a command to a variable. Each of the following examples demonstrates the command substitution − #!/bin/sh DATE=`date` echo "Date is $DATE" USERS=`who | wc -l` echo "Logged in user are $USERS" UP=`date ; uptime` echo "Uptime is $UP"


2 Answers

In bash, brace expansion happens before variable expansion, so this is not directly possible.

Instead, use an arithmetic for loop:

start=1 end=10 for ((i=start; i<=end; i++)) do    echo "i: $i" done 

OUTPUT

i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 i: 8 i: 9 i: 10 
like image 101
anubhava Avatar answered Oct 10 '22 01:10

anubhava


You should consider using seq(1). You can also use eval:

eval echo {$start..$end} 

And here is seq

seq -s' ' $start $end 
like image 29
cnicutar Avatar answered Oct 10 '22 00:10

cnicutar