Skip to content

Instantly share code, notes, and snippets.

@5HT2
Last active October 15, 2023 07:53
Show Gist options
  • Save 5HT2/30f98284e9f92e1b47b4df6e05a063fc to your computer and use it in GitHub Desktop.
Save 5HT2/30f98284e9f92e1b47b4df6e05a063fc to your computer and use it in GitHub Desktop.

genpasswd

mkdir -p ~/.local/bin
wget -O ~/.local/bin/genpasswd https://gist.githubusercontent.com/5HT2/30f98284e9f92e1b47b4df6e05a063fc/raw/9b5479444845c9866a227a0bdef31dc116a546df/genpasswd.sh
chmod +x ~/.local/bin/genpasswd

# Please make sure that ~/.local/bin is in your $PATH
echo "$PATH" | tr ':' '\n'

# If not, add the following to your shell's config, e.g. ~/.bashrc / ~/.zshrc
export PATH="$HOME/.local/bin:$PATH"
#!/bin/bash
# Usage:
# genpasswd
# genpasswd 10 yes
# genpasswd yes
# genpasswd 48 yes
regex="a-zA-Z0-9"
numberRegex="^[0-9]+$"
if [[ "$1" =~ $numberRegex ]]; then
l="$1"
elif [[ "$2" =~ $numberRegex ]]; then
l="$2"
else
l="32"
fi
if [[ "$1" == yes ]] || [[ "$2" == yes ]]; then
regex="${regex}!@#$%^&;:"
fi
# Fix for macOS "tr: Illegal byte sequence"
export LC_ALL=C
cat /dev/urandom | tr -dc "$regex" | fold -w "$l" | head -n 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment