Is there a standard unix program that returns a range of numbers
Im learning shell scripting from an outdated textbook, and it seems to me like it'd be really usefull to have a program that just returns a string of numbers delimited by spaces something like
$ range 10 20
10 11 12 13 14 15 16 17 18 19 20
Then, if youre doing a shell script you can have
for i in `range 10 20`; do some stuff with numbers in that range;done
does such a thing exist, or do I need to write it myself?
seq
is part of coreutils.
for i in $( seq 1 2 11 ) ; do echo $i ; done
Output:
1
3
5
7
9
11
If you provide only 2 arguments to seq
, the increment is 1:
$ seq 4 9
4
5
6
7
8
9
Would Bash suffice?
for i in {10..20}; do echo $i; done
You can do a lot of things with brace expansion. Bash 4 also supports padded ranges, e.g. {01..20}
.
Note that Bash is not considered portable, and not a standard Unix utility. Although you can safely assume that it is installed on most modern Linuxes, don't use this in a script that you plan to run on all kinds of Unix-like machines.
If you want something strictly portable (i.e. that does not rely on specific bash extensions or commands not specified by POSIX)
awk 'BEGIN {for(i=10;i<=20;i++) printf "%d ",i; print}'
Before 10.7 there was no seq
on Mac OS X, but jot
, due to the BSD heritage.
jot -- print sequential or random data
...
HISTORY
The jot utility first appeared in 4.2BSD
Example:
$ jot - 1 3
1
2
3