Intro To 'seq' Command In Linux
2024-01-24 - By Robert Elder
I use the 'seq' command to print a sequence of numbers:
seq 1 10
1
2
3
4
5
6
7
8
9
10
Default Behaviour Of 'seq' Command
When I run the 'seq' command with a single numerical argument, it will print all integers from 1, to the specified number:
seq 4
1
2
3
4
seq 9
1
2
3
4
5
6
7
8
9
seq 13.4
1
2
3
4
5
6
7
8
9
10
11
12
13
Print A Range Of Numbers
If I run the 'seq' command with two numerical arguments, the output will consist of the first number incremented by one up until a value not exceeding the second number:
seq 2 10
2
3
4
5
6
7
8
9
10
seq 3.3 6.2
3.3
4.3
5.3
Specify An Increment Value
If I provide three numerical arguments, the middle number allows me to specify the increment value between each number:
seq 1 2 10
1
3
5
7
9
seq 1 0.1 2
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0
Specify A Custom Separator
By default, the numbers are separated by a newline, but you can use the '-s' flag to specify a different delimiter:
seq -s ':' 1 10
1:2:3:4:5:6:7:8:9:10
seq -s '-' 5 20
5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20
seq -s '_' 4 14
4_5_6_7_8_9_10_11_12_13_14
Enforce Equal Width Zero Padding
You can also use the '-w' flag to cause all numbers to be printed with equal width zero padding:
seq 98 103
98
99
100
101
102
103
seq -w 98 103
098
099
100
101
102
103
Custom Format Strings
The '-f' allows you to specify a printf-style format string that will format the printed numbers accordingly:
seq -f 'Lowercase Hexadecimal Float: %.7a' -1 0.4 1
seq -f 'Uppercase Hexadecimal Float: %.7A' -1 0.4 1
Lowercase Hexadecimal Float: -0x8.0000000p-3
Lowercase Hexadecimal Float: -0x9.999999ap-4
Lowercase Hexadecimal Float: -0xc.ccccccdp-6
Lowercase Hexadecimal Float: 0xc.ccccccdp-6
Lowercase Hexadecimal Float: 0x9.999999ap-4
Lowercase Hexadecimal Float: 0x8.0000000p-3
Uppercase Hexadecimal Float: -0X8.0000000P-3
Uppercase Hexadecimal Float: -0X9.999999AP-4
Uppercase Hexadecimal Float: -0XC.CCCCCCDP-6
Uppercase Hexadecimal Float: 0XC.CCCCCCDP-6
Uppercase Hexadecimal Float: 0X9.999999AP-4
Uppercase Hexadecimal Float: 0X8.0000000P-3
seq -f 'Lowercase Scientific Notation %e' 10000 10000 100000
seq -f 'Uppercase Scientific Notation %E' 10000 10000 100000
Lowercase Scientific Notation 1.000000e+04
Lowercase Scientific Notation 2.000000e+04
Lowercase Scientific Notation 3.000000e+04
Lowercase Scientific Notation 4.000000e+04
Lowercase Scientific Notation 5.000000e+04
Lowercase Scientific Notation 6.000000e+04
Lowercase Scientific Notation 7.000000e+04
Lowercase Scientific Notation 8.000000e+04
Lowercase Scientific Notation 9.000000e+04
Lowercase Scientific Notation 1.000000e+05
Uppercase Scientific Notation 1.000000E+04
Uppercase Scientific Notation 2.000000E+04
Uppercase Scientific Notation 3.000000E+04
Uppercase Scientific Notation 4.000000E+04
Uppercase Scientific Notation 5.000000E+04
Uppercase Scientific Notation 6.000000E+04
Uppercase Scientific Notation 7.000000E+04
Uppercase Scientific Notation 8.000000E+04
Uppercase Scientific Notation 9.000000E+04
Uppercase Scientific Notation 1.000000E+05
seq -f 'Lowercase Float %f' 0.5 0.1 0.8
seq -f 'Uppercase Float %F' 0.5 0.1 0.8
Lowercase Float 0.500000
Lowercase Float 0.600000
Lowercase Float 0.700000
Lowercase Float 0.800000
Uppercase Float 0.500000
Uppercase Float 0.600000
Uppercase Float 0.700000
Uppercase Float 0.800000
seq -f 'Shortest of (%%e or %%f) %g' 999998 1000001
seq -f 'Shortest of (%%E or %%F) %G' 999998 1000001
Shortest of (%e or %f) 999998
Shortest of (%e or %f) 999999
Shortest of (%e or %f) 1e+06
Shortest of (%e or %f) 1e+06
Shortest of (%E or %F) 999998
Shortest of (%E or %F) 999999
Shortest of (%E or %F) 1E+06
Shortest of (%E or %F) 1E+06
Numerical Imprecision Errors
The 'seq' command can suffer from floating point and integer precision errors. For example, the output from this 'seq' command includes the value '1.0000000000000000007' twice:
seq 1 0.0000000000000000001 1.0000000000000000009
1.0000000000000000000
1.0000000000000000001
1.0000000000000000002
1.0000000000000000003
1.0000000000000000004
1.0000000000000000005
1.0000000000000000007
1.0000000000000000007
1.0000000000000000008
1.0000000000000000009
And the output from this 'seq' command includes the value '50000000000000000000' twice while completely omitting the expected value '50000000000000000002':
seq 50000000000000000000 2 50000000000000000004
50000000000000000000
50000000000000000000
50000000000000000004
And that's why the 'seq' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|