Craft: Howto: Iterate Bash For Loop Vari-Able Range Under Unix / Linux
Craft: Howto: Iterate Bash For Loop Vari-Able Range Under Unix / Linux
nixCraft
Linux and Unix tutorials for new and seasoned sysadmin
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?
#!/bin/bash
for i in {1..5}
do
echo "$i"
done
#!/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"
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
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
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 )
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
6 comment
Hi.
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...
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 :-)
‘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.
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...
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.
*****
****
***
**
*
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...
#!/bin/bash
for((i = 1; i = i; c--))
do
echo -n "*"
done
echo
done
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
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