Americas

  • United States
sandra_henrystocker
Unix Dweeb

What the jot command can do for you

How-To
Jun 12, 20174 mins
Data CenterLinux

The jot command has been around for ages, but remains one of those interesting commands that a lot of Linux users never get around to using. It can be very handy in scripts as well as on the command line by generating number or character sequences, even pseudo-randomly.

In its simplest form, the jot command generates a simple sequence of numbers from 1 to your selected maximum.

$ jot 5
1
2
3
4
5

You can stick the jot command output into simply by redirecting it.

$ jot 5 > five
$ cat five
1
2
3
4
5

If you want to start with some number other than 1, you just use a slightly different syntax. The command “jot 5 11”, for example, would create a list of five numbers starting with 11.

$ jot 5 11
11
12
13
14
15

In a script, you might fold the jot command into your for loop like this:

for i in `jot 10 1`; do echo $i; done

Here is an example:

#!/bin/bash

echo "Please enter your top 10 concerns"
if [ -f $USER-answers ]; then
  rm $USER-answers
fi

for num in `jot 10`
do
  echo -n "$num: "
  read ans
  echo $num: $ans >> $USER-answers
done

You can generate numbers that are not sequential by specifying the difference that you want to see between your numbers. Read the command below as “generate four numbers between 5 and 20”.

$ jot 4 5 20
5
10
15
20

When needed, the jot command will space out your numbers as well as it can.

$ jot 6 5 27
5
9
14
18
23
27

If you ask for more numbers than your range allows, jot will repeat numbers to satisfy your request.

$ jot 5 11 13
11
12
12
12
13

Plu, there’s nothing to stop you from generating sequences that go from large numbers to small. Just put the largest number in your range first.

$ jot 5 24 2
24
18
13
8
2

And, if you want to generate a list of numbers without having to first figure out how many numbers there will be, you can use the – option. Read this command as “generate a list of numbers between 78 and 93 that are 3 digits apart”. You don’t have to calculate how many numbers will end up on your list.

$ jot - 78 93 3
78
81
84
87
90
93

You can also opt for greater precision by specifying decimal places in one or both of your range specifications. You’d get the same output with “24.0 27” or “24.0 27.0” as with “24 27.0”.

$ jot 5 24 27.0
24.0
24.8
25.5
26.2
27.0

Another way to request higher precision numbers is to use the -p option to say how many decimal places you want to see.

$ jot -p2 5 5 30
5.00
11.25
17.50
23.75
30.00

Beyond numbers

You can also use jot to generate letter sequences.

$ jot -c 5 65
A
B
C
D
E

In this command, we’re telling jot to generate 5 characters (-c), starting with 65. What is 65, you ask? It’s the decimal value of the letter A (i.e., if we were to interpret that byte numerically). We could use a command like this one to verify that.

$ jot 1 A
65

A lowercase a falls into a higher range:

$ jot 1 a
97

To generate the alphabet in capital letters:

$ jot -c 26 65
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

The lowercase equivalent would require a similar command. Here, we’re asking for 26 letters starting with a (i.e., 97):

$ jot -c 26 97
a
b
c
…

We could, however, make this even easier on ourselves by just specifying the letter rather than its numeric value. Note that I’m just asking for the first five letters of the alphabet with the 5 argument.

$ jot -c 5 a
a
b
c
d
e

The “jot -c 26 A: would do the same thing for uppercase letters.

Going random

If you want to generate random numbers, you would use the -r option with opt. Here we generate 10 numbers between 1 and 1000:

$ jot -r 10 1 1000
6
596
428
472
757
898
112
402
914
48

Repeating yourself

You can also use jot to repeat a string as many times as you might like using the -b (word) option. As you can see, you can use multiple words.

$ jot -b "enough already!" 5
enough already!
enough already!
enough already!
enough already!
enough already!

Wrap-up

The jot command is interesting and could make some things you need to do, especially in your scripts, considerably easier. It probably helps, though, to have some example commands on hand to help you remember what each argument represents.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.