The Computer Oracle

Is there a standard unix program that returns a range of numbers

--------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Magic Ocean Looping

--

Chapters
00:00 Question
00:36 Accepted answer (Score 80)
01:03 Answer 2 (Score 34)
01:38 Answer 3 (Score 9)
01:58 Answer 4 (Score 6)
02:18 Thank you

--

Full question
https://superuser.com/questions/600667/i...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #unix

#avk47



ACCEPTED ANSWER

Score 79


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



ANSWER 2

Score 34


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.




ANSWER 3

Score 9


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}'



ANSWER 4

Score 2


Use a for loop

for ((i = 10; i <= 20; ++i)); do
    printf '%d\n' "$i"
done