Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

To generate random alphanumeric strings drop the following into your zshrc:

  function mkpw () {
      if (( $# == 0 )) then
          head /dev/urandom | uuencode -m - | sed -n 2p | cut -c1-${1:-12}
      else
          head /dev/urandom | uuencode -m - | sed -n 2p | cut -c1-${1:-$1}
      fi
  }
By default it generates an alphanumeric string of length 12. Given an integer argument n it generates an alphanumeric string of length n.


Why the test of both $# and $1?

    tr -dc '!-~' </dev/urandom | head -c ${1-12}; echo
Change to A-Za-z0-9, etc., to suit.


Mostly because I'm terrible at shell scripting. Thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: