-sh

Non-trivial command-line fu

pwgen() { LC_CTYPE=C tr -dc 'A-Za-z0-9\`~!@#$%^&*()_+[]{}\|;:<>?,./' </dev/urandom | head -c ${1:-16} | xargs; }

— -sh (@rtfmsh) February 22, 2013

Wait, what, you don't use 1Password or a similar tool to generate / manage passwords? It's alright, it has its limits, and every once in a while, I need to quickly come up with a reasonable password, and this shell function makes it easy.

Things to note here -- aside from the otherwise underappreciated tr(1) -- are the default behaviour of xargs(1) (i.e., it invokes echo(1); as in /bin/echo, not the shell builtin), and the use of the LC_CTYPE environment variable.

Setting LC_CTYPE is necessary, since nowadays some systems default to a UTF8 locale, which makes tr(1) unhappy, as it's attempting to interpret some of the byte sequences from /dev/urandom as illegal character sequences.

2013-02-22


[previous] [Index] [next]