#!/bin/bash
#                 /usr/local/bin/random_alphanum
# http://crystalfaeries.net/posix/bin/random_alphanum
# celeste:crystalfaery 2014-04-07 18:16:31+00:00

# Single optional argument is the count of characters to generate, defaulting to 16
if [ "X$1" == "X" ]
then
	count=16				# default to 16 characters
else
	count=`echo "$1" | tr -dc '[:digit:]'`	# sanitize input argument integer number of characters
fi

echo "`tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c${1:-$count}`"


syntax highlighted by Code2HTML, v. 0.9.1