Categories

# How to generate sequence of numbers

Generate and print sequences of numbers to perform specific operations using command-line or shell script.

### Basic usage

Print sequence of numbers from 1 to 10.

```\$ seq 1 10
```
```1
2
3
4
5
6
7
8
9
10
```

Print sequence of numbers from 10 to 1.

```\$ seq 10 1
```
```10
9
8
7
6
5
4
3
2
1
```

Print sequence of numbers from 1 to 10 using step size 0.5.

```\$ seq 1 0.5 10
```
```1
1.5
2
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7
7.5
8
8.5
9
9.5
10
```

Print sequence of numbers from 1 to 10 and equalize width by padding with leading zeroes.

```\$ seq --equal-width 1 10
```
```01
02
03
04
05
06
07
08
09
10
```

Print sequence of numbers from 1 to 10 and ensure that width is at least two characters wide by padding with leading zeroes.

```\$ seq --format "%02g" 1 10
```
```01
02
03
04
05
06
07
08
09
10
```

### Examples

Pretty-print sequence of numbers from 250 to 260 using the hexadecimal format.

```\$ seq 250 260 | xargs printf '0x%04x\n'
```
```0x00fa
0x00fb
0x00fc
0x00fd
0x00fe
0x00ff
0x0100
0x0101
0x0102
0x0103
0x0104
```

Pretty-print sequence of numbers from 0 to 16 and their hexadecimal counterpart.

```\$ seq 0 16 | xargs -I N printf '%02d: 0x%02x\n' N N
```
```00: 0x00
01: 0x01
02: 0x02
03: 0x03
04: 0x04
05: 0x05
06: 0x06
07: 0x07
08: 0x08
09: 0x09
10: 0x0a
11: 0x0b
12: 0x0c
13: 0x0d
14: 0x0e
15: 0x0f
16: 0x10
```

Check how many rotated logs exist for syslog log file.

```\$ seq 20 | xargs -I {} -i bash -c '(test -f /var/log/syslog.{} || test -f /var/log/syslog.{}.gz) && echo "there is syslog.{} rotated log"'
```
```there is syslog.1 rotated log
there is syslog.2 rotated log
there is syslog.3 rotated log
there is syslog.4 rotated log
there is syslog.5 rotated log
there is syslog.6 rotated log
there is syslog.7 rotated log
```

Display the current month and mark the current day using a shell script.

```#!/bin/bash
current_year=\$(date +%Y)
current_month=\$(date +%m)
current_day=\$(date +%d)
days_in_current_month=\$(cal \$current_month \$current_year | awk 'NF {DAYS = \$NF}; END {print DAYS}')

for day in \$(seq -f %02g 01 \$days_in_current_month); do
printf  "\${current_year}-\${current_month}-\${day}"
if [ "\$day" -eq "\$current_day" ]; then
printf " <--- You are here"
fi
echo
done```
```2017-12-01
2017-12-02
2017-12-03
2017-12-04
2017-12-05
2017-12-06
2017-12-07
2017-12-08 <--- You are here
2017-12-09
2017-12-10
2017-12-11
2017-12-12
2017-12-13
2017-12-14
2017-12-15
2017-12-16
2017-12-17
2017-12-18
2017-12-19
2017-12-20
2017-12-21
2017-12-22
2017-12-23
2017-12-24
2017-12-25
2017-12-26
2017-12-27
2017-12-28
2017-12-29
2017-12-30
2017-12-31
```