Generate passwords that need to fit character class and length requirements

(, en)

In case you need to generate passwords for Azure:

#!/usr/bin/env bash

pw_length=${1:-18}
p=''
while [[ !("$p" =~ [A-Z] && "$p" =~ [a-z] && $p =~ [0-9] && "$p" =~ [[:punct:]]) ]]; do
  p=$(LC_ALL=C < /dev/urandom tr -dc "[:graph:]" | tr -d "'" | head -c${pw_length})
done

echo -n $p