- 구글 클라우드, 구글 워크스페이스용 제미나이 사이드 패널에 한국어 지원 추가
- The best MagSafe accessories of 2024: Expert tested and reviewed
- Threads will show you more from accounts you follow now - like Bluesky already does
- OpenAI updates GPT-4o, reclaiming its crown for best AI model
- Nile unwraps NaaS security features for enterprise customers
Using the seq command to generate numeric sequences
The seq command is handy for generating number sequences on Linux.
It requires at least one number as an argument and, given that, it will provide all the numbers from 1 to whatever number you specify. However, that’s just the command’s simplest use. With additional arguments, seq can be surprisingly useful, especially when you use the generated numbers to drive a loop or do a calculation.
The seq command below illustrates the most basic use of this command:
$ seq 5 1 2 3 4 5
The seq command is also surprisingly fast. When I ask it to show me all of the numbers from 1 to 1,000, it only takes a few seconds to run. The bottom of the number list and timing stats are shown below.
$ time seq 1000 | tail -8 993 994 995 996 997 998 999 1000 real 0m0.003s user 0m0.001s sys 0m0.003s
When you use two arguments with seq, they will be used as the starting and ending numbers in the generated list. Notice that there is no need to start with 1.
$ seq 11 17 $ seq 111 117 $ seq 1234 1240 11 111 1234 12 112 1235 13 113 1236 14 114 1237 15 115 1238 16 116 1239 17 117 1240
You can also specify an increment, so that you only see every Xth number in your series. If you provide a starting number, an increment and an ending number in that order, you can tailor your numbers to suit some particular purpose. Say you want to see all of the leap years between 2024 and 2054. Knowing that 2024 is a leap year, you could use a command like this:
$ seq 2024 4 2055 2024 2028 2032 2036 2040 2044 2048 2052
That “seq FIRST INCREMENT LAST” syntax gives you a lot of control over the numbers that will be listed.
If you want your list to start with the largest number and end with the smallest, use a negative increment (in other words, a “decrement”) like this:
$ seq 100 -10 0 100 90 80 70 60 50 40 30 20 10 0
To use the seq command to drive a bash loop, you could use a command like that shown below. The sequence is the list of leap years shown above.
$ for yr in `seq 2024 4 2055` > do > echo "$yr is a leap year" > done 2024 is a leap year 2028 is a leap year 2032 is a leap year 2036 is a leap year 2040 is a leap year 2044 is a leap year 2048 is a leap year 2052 is a leap year
If you want to generate a list of numbers and pad the smaller ones with zeroes so that all of the numbers in your list have the same number of digits, add the -w option like this:
$ seq -w 1 11 100 001 012 023 034 045 056 067 078 089 100
Each number in the list will have the same number of digits as the largest number.
You can also specify how many zeroes to use as padding. In this next example, we want every number to have at least three digits.
$ seq -f "%03g" 10 001 002 003 004 005 006 007 008 009 010
You can also ask the seq command to use numbers with decimal points as in this example:
$ seq 0.3 0.3 9.9 | column 0.3 1.5 2.7 3.9 5.1 6.3 7.5 8.7 9.9 0.6 1.8 3.0 4.2 5.4 6.6 7.8 9.0 0.9 2.1 3.3 4.5 5.7 6.9 8.1 9.3 1.2 2.4 3.6 4.8 6.0 7.2 8.4 9.6
Notice that the starting number, increment, and final number all have decimal points. This command starts with 0.3 and adds 0.3 until it reaches 9.9.
To get the seq command to generate the separators instead of passing the output to the column command, you could do this:
$ seq -s " " 0.3 0.3 9.9 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 3.0 3.3 3.6 3.9 4.2 4.5 4.8 5.1 5.4 5.7 6.0 6.3 6.6 6.9 7.2 7.5 7.8 8.1 8.4 8.7 9.0 9.3 9.6 9.9
This output consists of one long line of numbers, wrapped around as needed to fit your terminal window.
You can use other characters as separators too. Here’s an example using the colon character as the separator when generating a list of leap years:
$ seq -s: 2024 4 2050 2024:2028:2032:2036:2040:2044:2048
The command below uses a comma as the separator:
$ seq -s, 100 -10 0 100,90,80,70,60,50,40,30,20,10,0
If you’re curious about how big a number multiplying each of the numbers 1 through 10 would be, use you use a command like this:
$ seq -s* 1 10 | bc 3628800
That seq command generates the string 1*2*3*4*5*6*7*8*9*10 because we used an asterisk as the separator. When this string is then passed to the calculator, bc, we get the number shown. To add the numbers 1-10 together, use a + sign instead of an *.
$ seq -s + 1 10 | bc 55
Using tabs as separators is a bit tricky, but you can force it with a command like this one:
$ seq -s "`echo -e "t"`" 5 11 5 6 7 8 9 10 11
Copyright © 2022 IDG Communications, Inc.