0% found this document useful (0 votes)
44 views8 pages

Craft: Howto: Iterate Bash For Loop Vari-Able Range Under Unix / Linux

This document discusses different ways to iterate through a range of numbers using a for loop in Bash. It explains that using curly braces to define a range like {1..5} will not work if the range is stored in variables. As alternatives, it recommends using a 3-expression for loop syntax from C, a while loop, or using eval to concatenate the variable range. It also provides an example of iterating through an array.

Uploaded by

Mauca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

Craft: Howto: Iterate Bash For Loop Vari-Able Range Under Unix / Linux

This document discusses different ways to iterate through a range of numbers using a for loop in Bash. It explains that using curly braces to define a range like {1..5} will not work if the range is stored in variables. As alternatives, it recommends using a 3-expression for loop syntax from C, a while loop, or using eval to concatenate the variable range. It also provides an example of iterating through an array.

Uploaded by

Mauca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

nixCraft
Linux and Unix tutorials for new and seasoned sysadmin

HowTo: Iterate Bash For Loop Vari-


able Range Under Unix / Linux
last updated April 20, 2012 in BASH Shell

H
ow can I iterate bash for loop using a variable range of
numbers in Unix or Linux or BSD or Apple OS X operating
systems?

You can use the following syntax for setting up ranges:

#!/bin/bash
for i in {1..5}
do
echo "$i"
done

However, the following will not work:

#!/bin/bash
START=1

1 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

END=5
for i in {$START..$END}
do
echo "$i"
done

Recommended solution

To �x this problem use three-expression bash for loops syntax which share a common
heritage with the C programming language. It is characterized by a three-parameter
loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2),
and a counting expression (EXP3):

#!/bin/bash
START=1
END=5
echo "Countdown"

for (( c=$START; c<=$END; c++ ))


do
echo -n "$c "
sleep 1
done

echo
echo "Boom!"

Sample outputs:

Countdown
1 2 3 4 5
Boom!

while…do..done

Another option is to use the bash while statement which is used to execute a list of
commands repeatedly:

2 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

#!/bin/bash
START=1
END=5
## save $START, just in case if we need it later ##
i=$START
while [[ $i -le $END ]]
do
echo "$i"
((i = i + 1))
done

Fixing the original code with eval

With eval builtins the arguments are concatenated together into a single command,
which is then read and executed:

#!/bin/bash
START=1
END=5
for i in $(eval echo "{$START..$END}")
do
echo "$i"
done

A note about iterate through an array

The Bash shell support one-dimensional array variables and you can use the following
syntax to iterate through an array:

#!/bin/bash

## define an array ##
arrayname=( Dell HP Oracle )

## get item count using ${arrayname[@]} ##


for m in "${arrayname[@]}"
do
echo "${m}"
# do something on $m #
done

Sample outputs:

3 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

Dell
HP
Oracle

Of�cial Linux Foundation Certi�cations and Training - 15% Off


Get 15% off on Linux Foundation certi�ed SysAdmin, Progamming,
Kubernetes/Containers and Open Stack certi�cation & course. Use "CYBER15" coupon
code.
training.linuxfoundation.org

Posted by: Vivek Gite


The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer
for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin,
Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

GOT FEEDBACK? CLICK HERE TO JOIN THE DISCUSSION

6 comment

xeuler May 4, 2012 at 3:06 am

Hi.

when i use {1..10000000} in

for x in {1..10000000} ; do : ; done


* do nothing

bash uses 2 GB of ram.

4 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

Is the same when i use seq

for i in $(seq 1 10000000); do :; done

The memory is still in use until i close de session (terminal).

it’s like fork bom xp

Mark S May 15, 2012 at 6:04 pm

When working with contiguous numerical ranges that increase, I �nd the command
‘seq’ to be more ef�cient and powerful. For example, let’s go from 0 to 20 by increments
of 3 and output some �bonacci stylings :-)

for i in `seq 0 3 20`


do
((j = $j + $i))
echo $j
done

‘seq’ also has a few options such as padding with leading zeros, changing the �eld
separator and changing the printf format. The -w for leading zeros comes in handy
frequently. Issue ‘man seq’ for the details.

Xiaofeng July 12, 2012 at 2:26 am

hi VIVEK, could u tell me why your second sample didn’t work? it’s unusual to my
intuition. and not easy to understand.

5 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

Jason September 26, 2012 at 2:21 am

There is incorrect information in the last example. It says:

## get item count using ${arrayname[@]} ##

This only concatenates the items of the array together separated by whitespace (which
works for the example). But to get the count of an array,

${#arrayname[@]}

or

${#arrayname[*]}

is used.

tushar November 15, 2015 at 3:43 pm

*****
****
***
**
*
how i make this using for loop

6 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

Odd Tom January 2, 2017 at 9:57 pm

#!/bin/bash
for((i = 1; i = i; c--))
do
echo -n "*"
done
echo
done

    Have a question? Post it on our forum!

Tagged as: apple os x, array variables, bash for loop, bash for loop example, BASH Shell, bsd,
c programming language, c sleep, control expression, dimensional array, loop control,
operating systems, oracle, shell support, variable range

@2000-2018 nixCraft. All rights reserved.

PRIVACY

TERM OF SERVICE

CONTACT/EMAIL

DONATIONS

7 of 8 8/24/18, 7:43 PM
HowTo: Iterate Bash For Loop Variable Range Un... https://www.cyberciti.biz/faq/unix-linux-iterate-ov...

SEARCH

Hosted by Linode
DNS & CDN by Cloud�are
Designed and Developed by Prospect One

8 of 8 8/24/18, 7:43 PM

You might also like