dotfiles/pwgen.sh
2025-05-30 16:25:30 +02:00

21 lines
550 B
Bash
Executable file

#!/bin/bash
# Function to generate a random password
generate_password() {
local length=$1
if ! [[ "$length" =~ ^[0-9]+$ ]] || [ "$length" -le 0 ]; then
echo "Please provide a valid positive integer for password length."
exit 1
fi
local charset='A-Z:a-z:0-9:!@#$%^&*()_+[]{}|;:,.<>?'
# Generate the password
local password=$(cat /dev/urandom | tr -dc "$charset" | fold -w "$length" | head -n 1)
echo "$password"
}
if [ $# -ne 1 ]; then
echo "Usage: $0 <length>"
exit 1
fi
generate_password "$1"