Approach 1
Simply augment the command within $(...)
with another call to seq
:
for i in $(seq 0 5 15; seq 30 5 45); do
echo $i
done
and then
$ bash test.sh
0
5
10
15
30
35
40
45
# Approach 2
In your follow-up comment, you write
The actual content of the for loop is more than just echo$i
(about 200 lines) I don't want to repeat it and make my script huge!
As an alternative to the approach outlined above, you could define a shell function for those 200 lines and then call the function in a series of for
loops:
f() {
# 200 lines
echo $i
}
for i in $(seq 0 5 15) do
f
done
for i in $(seq 30 5 45) do
f
done
Approach 3 (POSIX-compliant)
For maximum portability across shells, you should make your script POSIX-compliant. In that case, you need have to eschew seq
, because, although many distributions provide that utility, it's not defined by POSIX.
Since you can't use seq
to generate the sequence of integers to iterate over, and because POSIX doesn't define numeric, C-style for
loops, you have to resort to a while
loop instead. To avoid duplicating the code related to that loop, you can define another function (called custom_for_loop
below):
custom_for_loop () {
# $1: initial value
# $2: increment
# $3: upper bound
# $4: name of a function that takes one parameter
local i=$1
while [ $i -le $3 ]; do
$4 $i
i=$((i+$2))
done
}
f () {
printf "%s\n" "$1"
}
custom_for_loop 0 5 15 f
custom_for_loop 30 5 45 f
seq 0 5 45
and check that it's not 20 or 25 at the top andcontinue
if it is? – Christoperchristophseq
should rarely, if ever, be used in a shell script. Most modern shells have more efficient alternatives (such as C-style for loops), and a script that requires POSIX compatibility cannot useseq
because it is not defined by POSIX. (You would use awhile
loop and update the index yourself instead.) – Yeaton