Generating passwords in the terminal: what works and what fails without telling you

Published on by David Carrero

In May a tutorial appeared on Medium with a title that is hard to ignore: Generate Unbreakable Passwords in Your Terminal Using Pure Bash. The core idea is a good one and we share it: the terminal already ships with an excellent password generator, there is nothing to install and the password never leaves your machine. It is the same case this site makes for the generator in your browser, just without the browser.

So we copied its commands and ran them on a Mac, on 10 November 2026, with macOS 27.2 and the system locale set to Spanish. And on Linux, for comparison. What works, works very well. What fails, in several cases, fails without saying a word, which is the worst thing that can happen to a password generator.

Where the randomness comes from

The tutorial opens with what really matters: openssl rand -base64 32 gives you 32 random bytes encoded in base64, and /dev/urandom is the source almost every other command draws from.

One caveat, because the tutorial makes it sound a little more romantic than it is. Neither openssl rand nor /dev/urandom hands you “pure physical randomness”: both are a cryptographic generator, a CSPRNG, which the operating system seeds with real noise from the hardware and which from then on produces bytes nobody can predict. OpenSSL says so in its documentation: it is a deterministic generator of the NIST SP 800-90A model that seeds and reseeds itself from the system. Linux uses one based on ChaCha20; macOS, according to Apple’s security guide, one derived from Fortuna.

That is not a flaw, it is the reason it works: a well-seeded CSPRNG is exactly what you want. It is the same kind of source as crypto.getRandomValues(), the one used by this site’s generator.

And there is something the tutorial gets right without making a point of it, when it should: the tr -dc trick, keeping only the bytes that fall inside the alphabet and throwing the rest away, introduces no bias. It is the correct approach. The naive way, taking the byte and using the remainder of dividing by the size of the alphabet, favours some characters over others; discarding does not.

On a Mac, the command returns one letter

This is the tutorial’s command for a password made of letters and digits:

cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 32

On the Mac, with the locale set to Spanish, this is what comes out:

tr: Illegal byte sequence

And below it, an E. A single letter. The exit code is 0: as far as the terminal is concerned, everything went fine.

The cause is that macOS’s tr reads its input as text in your locale’s encoding, UTF-8, and random bytes almost never form valid UTF-8. As soon as it hits the first one that does not fit, it stops. Whatever it had filtered up to that point, sometimes nothing, sometimes a couple of characters, is what reaches head, and head has no way of knowing it was expecting 32.

It does not happen on Linux: GNU tr works on bytes and ignores the locale. We tried it in a Debian container and all 32 characters came out. That is why a tutorial written and tested on Linux can be perfectly fine for the person who wrote it and still break on the reader’s Mac.

The fix is a variable in front: LC_ALL=C tr -dc …, which tells tr to treat its input as bytes. But what matters is not the fix, it is the shape of the failure. The genpass function the tutorial suggests, dropped into a Mac’s .bashrc, prints that E as if it were a password. If you paste it without looking into a field with no minimum length, that is now your password.

The hyphen that was not a hyphen

For the “with symbols” version, the tutorial uses this alphabet:

tr -dc 'a-zA-Z0-9!@#$%^&*()_+-=[]{}'

The intention is clear: letters, digits and eighteen symbols. But to tr, a hyphen between two characters is a range, just like in a-z. So +-= does not mean “plus, hyphen, equals”: it means everything between + and = in the ASCII table, which includes the comma, the full stop, the slash, the ten digits, the colon, the semicolon and the less-than sign.

We measured it by generating two million bytes and keeping the distinct characters that came out: 86, not the 80 that were intended. The same on macOS as on Linux. It is not a security problem, since six extra characters give slightly more entropy, not less; it is a problem of knowing what you are generating. If the system the password is going into rejects ; or <, the command fails every now and then and there is no way to guess why. The rule is to put the hyphen at the end of the set, where tr reads it as a hyphen.

And we say this with some humility, because while writing the corrected version below we fell into a trap from the same family: we put the alphabet in as a parameter’s default value, ${2:-…{}-}, and the alphabet’s first closing brace closed the parameter. The result produced a | nobody had asked for and had lost the hyphen. We only noticed because we counted the characters coming out.

64 characters that are sometimes 58

The function’s default mode does this:

openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | head -c "$LENGTH"

48 bytes in base64 make 64 characters. But base64 includes + and /, and the filter throws them out, so what is left is somewhat less than 64. For the default length of 32 that is plenty. For genpass 64, it is not: we ran it 200 times and only 28 came out with 64 characters; the rest had between 58 and 63. No warning.

The hex mode has its own version of the same problem: it divides the length by two to work out the bytes, so genpass 15 hex returns 14.

The scorer that applauds P@ssw0rd

The tutorial ends with a script that scores passwords: points for length, one for having lowercase, another for uppercase, another for digits and two for symbols. We ran it on a few:

  • P@ssw0rd — STRONG
  • Summer2026! — STRONG
  • correct-horse-battery-staple — STRONG
  • the letter a 35 times — MODERATE

P@ssw0rd gets the same grade as four random words. It is one of the first guesses in any attack dictionary. The script cannot know that because it does not look at what the password is, only at what kinds of characters it is made of. It is exactly the “one uppercase letter, one number and one symbol” rule that NIST stopped recommending in 2017, and which we already covered in the password game.

There is a second, more practical problem: the script takes the password as an argument, ./scorepass.sh "P@ssw0rd". That leaves it written in your shell history, in ~/.zsh_history or ~/.bash_history, and, while it runs, visible to any other user on the machine who lists the processes. To test a real password, this site’s checker looks at patterns, dictionaries and substitutions such as @ for a, and it does so in your browser without sending anything.

How much is enough

The tutorial sells the 256 bits of openssl rand -base64 32 as “the same strength that protects banking transactions”. The figure is correct and the comparison is unnecessary: 256 bits is far more than a password needs. With the attack model used across the rest of the site, 10¹² guesses per second, offline and with a fast hash, 128 bits already takes 10¹⁹ years. There is nothing wrong with using 256, but “more” should not be confused with “necessary”. We go through it properly in how long a password takes to crack.

Two details about the formats, which are not about security but about use:

  • base64 is not URL-safe, whatever the tutorial says: it includes +, / and the trailing =. That = is padding, not randomness; the 256 bits travel in the 43 characters in front of it. There is a variant, base64url, that swaps + and / for - and _, but openssl rand -base64 does not use it.
  • Hexadecimal gives 4 bits per character, half of what an alphabet of letters, digits and symbols gives. openssl rand -hex 32 produces 64 characters and 256 bits: perfect for an API key. But genpass 16 hex gives 16 characters and 64 bits, which under that same attack model are cracked in about 107 days on average. It looks long; it is not that long.

The words, and an example that came from no command

The words mode pulls words from /usr/share/dict/words with shuf. On a Mac, shuf does not exist: it is a GNU tool and macOS does not ship it, so that mode simply does not work. The list is there, though, and it has 235,976 words, which would give about 71 bits with four. A lot. The problem is that many of them are words you have never seen, and the whole point of a passphrase is being able to remember it.

The sample output the tutorial shows, correct-horse-battery-staple, was not generated by any command: it is the phrase from XKCD comic 936, from 2011. That comic is right about what it says, that four words picked at random beat one word with substitutions, but the tutorial sums it up as “length always beats complexity”, and that is not what it says. What wins is the number of options the random choice was made from. Thirty-five a’s have length to spare and no entropy at all. If you want passphrases done properly, Diceware uses a list of 7,776 words chosen to be memorable, at 12.9 bits each.

A version that fails out loud

This is what we suggest. Before publishing it we tested it in bash and zsh on macOS, and in bash on Linux:

genpass() {
  local n="${1:-20}" set='A-Za-z0-9!@#%^&*()_+=-' pw
  [ -n "$2" ] && set="$2"
  pw=$(LC_ALL=C tr -dc "$set" < /dev/urandom | head -c "$n")
  [ "${#pw}" -eq "$n" ] || { echo "genpass: failed" >&2; return 1; }
  printf '%s\n' "$pw"
}

Three decisions, one for each of the failures above:

  • LC_ALL=C in front of tr, so that on a Mac it reads bytes and does not stop.
  • The hyphen at the end of the alphabet, so that it is a hyphen. We checked by counting: exactly the 75 characters written there come out, not one more.
  • The length check. If for whatever reason the password comes out shorter than requested, the function does not print it, it warns on standard error and returns a non-zero code. A generator failing is fine. Failing and handing you something anyway must never happen.

genpass gives 20 characters from that alphabet, about 124 bits. genpass 32 gives 32. genpass 6 0-9 gives a PIN. The alphabet deliberately leaves out $, the backtick and quotes: those are the characters a shell or a configuration file interprets, and a password that breaks when you paste it into a .env is not more secure, just more annoying.

What the tutorial gets right at the end

The tutorial’s last section is its best: generating is easy, the hard part is not reusing. A 256-bit password used on two sites is compromised on both the day either of them leaks. Its proposal, generate in the terminal and store in a manager such as Bitwarden or KeePass, is exactly the only rule that has survived.

One last warning about its other suggestion, copying to the clipboard with pbcopy or xclip. It is better than printing it on screen, but the clipboard is not a private place: clipboard managers keep a history, and on a Mac with Handoff turned on, what you copy can also show up on your iPhone. Copy, paste, and copy something else over it.


Sources: the tutorial “Generate Unbreakable Passwords in Your Terminal Using Pure Bash”, published on Medium on 12 May 2026 · every command was run on 10 November 2026 on macOS 27.2 (bash 3.2, zsh, the system tr, OpenSSL 4.1) and in a stable Debian container with GNU coreutils 9.7 · OpenSSL documentation, RAND(7): deterministic generator per NIST SP 800-90A Rev. 1, seeded from the operating system · Linux random(4) man page · Apple, Apple Platform Security, random number generation · XKCD 936, “Password Strength” (2011) · NIST SP 800-63B (2017) on composition rules · the attack model, 10¹² guesses per second offline with a fast hash, is the same as on the rest of the site.

Photo by Luis Gomes · Pexels

← Back to the blog