3 ms·
Shell One-Liners to Generate Random Passwords
- wahern 10y agoI don't think any of those are portable. base64 isn't available on OpenBSD, for example. md5sum isn't available on OS X, and while almost every system has a similar hash utility the names and output format vary. And it goes without saying that none of those are defined by POSIX. Here's what I've been using in my portable shell scripts to generate randoms strings od -An -N8 -tx1 -v /dev/urandom 2>>/dev/null | tr -cd '0123456789abcdef' That's worked reliably on all the Linux, *BSD, Solaris, AIX, OS X, and Minix systems I've tried. od is defined by POSIX and available everywhere, even most annoying embedded Linux environments that don't care about providing the basic POSIX shell utilities. 2>>/dev/null because, until I reported the bug, NetBSD's /bin/sh aborted when redirecting to /dev/null when noclobber was enabled. All my shell scripts start with set -e # strict errors set -u # don't expand unbound variables set -f # disable pathname expansion set -C # noclobber I also always put export LC_ALL=C at the top of my scripts. That might be prudent for others to do, as well. While it would be idiotic for od to be locale aware, some tr implementations too clever for their own good might interpret the input stream and the translation specifiers in terms of the current locale. That could lead to less output than expected.