Generating 2FA alike 6-digit Passcode with shuf
This is a one-liner to generate 2FA alike 6-digit passcode:
Let’s break it down.
To randomize 10 digits with shuf command, we can do either of the following:
The shuf command provides an
--input-range option to generate numeric random sequence. This reduces the need for another command. The last
echo command is used to add the newline.
If we just need a subset of them, such as 6 digits:
Again, shuf has another option
--head-count, which is similar to the head command:
The output looks like a 2FA passcode.
Let’s try a few iterations:
However, there is one problem. The digits are not repeatable.
This is easy to resolve. We can do it one digit at a time and repeat for six times:
To avoid calling
shuf multiple times, we can generate our data space first:
Let’s test out a few examples:
Now they look more like those 2FA passcodes.