The Computer Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Mysterious Puzzle

--

Chapters
00:00 Is There A Standard Unix Program That Returns A Range Of Numbers
00:28 Answer 1 Score 34
00:53 Accepted Answer Score 79
01:12 Answer 3 Score 9
01:27 Answer 4 Score 2
01:34 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