Need to generate a random a-zA-Z0-9 string in bash or sh? Well this little function should help.
You can passover the string length or leave it blank for an 8 character string.
Code
function randompass () { local randompassLength if [ $1 ]; then randompassLength=$1 else randompassLength=8 fi pass=</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength echo $pass }
Example Usage
#!/bin/bash # function randompass () { local randompassLength if [ $1 ]; then randompassLength=$1 else randompassLength=8 fi pass=</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength echo $pass } fourChrString=`randompass 4` eightChrString=`randompass` echo "4 character random string : $fourChrString" echo "8 character random string : $eightChrString"
Example Output
4 character random string : pnoc 8 character random string : c110EM21
very nice!
ReplyDeletethanx