How to Run the Same Command Multiple Times in Redis

The Redis CLI allows us to easily run a command multiple times. All we need to do is prefix the command with the number of times we want it to run.

Example

Suppose we run the following command:

INCR counter

Result:

(integer) 1

The INCR command increments a key by one. In my case, the counter key didn’t exist, and so the INCR command created it with a value of zero, and then incremented it by one.

We can run the INCR command multiple times by prefixing it with a number:

10 INCR counter

Result:

(integer) 2
(integer) 3
(integer) 4
(integer) 5
(integer) 6
(integer) 7
(integer) 8
(integer) 9
(integer) 10
(integer) 11

Here’s another example that uses the RANDOMKEY command:

5 RANDOMKEY

Result:

"colour"
"counter"
"listname"
"counter"
"lastname"

The RANDOMKEY command returns a random key from the database. In this example, I ran it five times.