Password Generator | Cododel
CODODELDEV
EN / RU
Back to Deck
[shell]

Password Generator

SOURCE Bash
VERSION 1.0
AUTHOR Cododel

A flexible Bash script for generating secure passwords with multiple customization options. Supports random passwords, memorable passwords (consonant-vowel pattern), and fine-grained control over character types.

Features

  • Customizable length: Default 16 characters, configurable via -n
  • Memorable mode: Alternating consonants and vowels for easy recall
  • Character types: Optional uppercase, digits, and special characters
  • Random replacement: Smart percentage-based character replacement
  • Zero dependencies: Pure Bash, works everywhere

Usage

Terminal window
# Basic usage (16 chars, lowercase only)
./passgen.sh
# Custom length
./passgen.sh -n 24
# With uppercase and digits
./passgen.sh -u -d
# Memorable password with special chars
./passgen.sh -m -s -n 20
# Full options (uppercase, digits, special)
./passgen.sh -n 32 -u -d -s

Options

FlagDescription
-n <length>Set password length (default: 16)
-uInclude uppercase letters
-dInclude digits (10% of length)
-sInclude special characters (15-30% of length)
-mGenerate memorable password (consonant-vowel pattern)
-hShow help message

Examples

Terminal window
# Simple 20-character password
$ ./passgen.sh -n 20
kqwjdnfgxtmplvhzrbcs
# Memorable password
$ ./passgen.sh -m -n 12
tokuvineqara
# Secure password with all options
$ ./passgen.sh -n 16 -u -d -s
aB9f*Gh2!Kl5+Mn8
# Long password for maximum security
$ ./passgen.sh -n 64 -u -d -s

How It Works

  1. Base generation: Creates lowercase string or memorable pattern
  2. Character replacement: Replaces random positions with:
    • Uppercase: 15-30% of length
    • Digits: ~10% of length
    • Special chars: 15-30% of length
  3. Output: Prints final password to stdout

Source Code

passgen.sh

#!/bin/bash
chars="abcdefghijklmnopqrstuvwxyz"
consonants='bcdfghjklmnpqrstvwxyz'
vowels='aeiou'
digits='0123456789'
special='()`~!@#$*-+={}[]:;,. ?/'
length=16
include_uppercase=false
include_special=false
generate_memorable=false
__help() {
cat << EOF
Usage: $(basename $0) [-n <length>] [-u] [-d] [-s] [-m] [-h]
Options:
-n <length> Set the length of the random string (default is 16)
-u Include uppercase letters in the random string
-d Include digits in the random string
-s Include special characters in the random string
-m Generate a memorable password
-h Show this help message and exit
EOF
exit 0
}
__generate_random_password() {
local length=${1:-12}
local password=""
local available_chars="$chars"
for ((i=0; i<length; i++)); do
password+=${available_chars:RANDOM%${#available_chars}:1}
done
echo "$password"
}
__generate_memorable_password() {
local length=${1:-12}
local password=""
local available_consonants="$consonants"
local available_vowels="$vowels"
for ((i=0; i<length; i++)); do
if (( i % 2 == 0 )); then
password+=${available_consonants:RANDOM%${#available_consonants}:1}
else
password+=${available_vowels:RANDOM%${#available_vowels}:1}
fi
done
echo "$password"
}
while getopts "n:udsmh" opt; do
case $opt in
n) length="$OPTARG" ;;
u) include_uppercase=true ;;
d) include_digits=true ;;
s) include_special=true ;;
m) generate_memorable=true ;;
h) __help; exit 0 ;;
*) __help; exit 1 ;;
esac
done
if [ "$generate_memorable" = true ]; then
res=$(__generate_memorable_password "$length")
else
res=$(__generate_random_password "$length")
fi
function handle_special_chars() {
local num_special_replacements=$((length * (15 + RANDOM % 16) / 100))
if [ "$num_special_replacements" -eq 0 ]; then
num_special_replacements=1
fi
for ((i=0; i<num_special_replacements; i++)); do
local index=$((RANDOM % length))
local random_special_char=${special:RANDOM%${#special}:1}
res="${res:0:index}$random_special_char${res:index+1}"
done
}
function handle_uppercase_chars() {
local num_uppercase_replacements=$((length * (15 + RANDOM % 16) / 100))
if [ "$num_uppercase_replacements" -eq 0 ]; then
num_uppercase_replacements=1
fi
for ((i=0; i<num_uppercase_replacements; i++)); do
local index=$((RANDOM % length))
local random_uppercase_char=$(echo "$chars" | tr '[:lower:]' '[:upper:]' | cut -c $((RANDOM % 26 + 1)))
res="${res:0:index}$random_uppercase_char${res:index+1}"
done
}
function handle_digits() {
local chance=1/10
local num_digits_replacements=$((length * chance))
if [ "$num_digits_replacements" -eq 0 ]; then
num_digits_replacements=1
fi
for ((i=0; i<num_digits_replacements; i++)); do
local index=$((RANDOM % length))
local random_digit=${digits:RANDOM%${#digits}:1}
res="${res:0:index}$random_digit${res:index+1}"
done
}
if [ "$include_special" = true ]; then handle_special_chars; fi
if [ "$include_uppercase" = true ]; then handle_uppercase_chars; fi
if [ "$include_digits" = true ]; then handle_digits; fi
echo "$res"
[ ▲ 0 ]