Description
genpass
is a simple Bash script for generating strong random passwords. It supports customizable password length, options to include numbers and symbols, and ensures that generated passwords always start and end with alphabetic characters to maintain compatibility with most systems.
Key features
- Customizable password length (default: 32 characters)
- Optional inclusion of numbers and symbols (enabled by default)
- Password always begins and ends with alphabetic characters
- Interactive mode when no arguments are provided
- Built-in help option (
--help
/-h
)
Installation
- Save the script to a file, e.g.,
genpass
. - Make it executable:
chmod +x genpass
- (Optional) Move it to a folder in your
$PATH
:sudo mv genpass /usr/local/bin/
Usage
General format
genpass [<length> <use_numbers> <use_symbols>]
Without arguments
The script will prompt for password length, number inclusion, and symbol inclusion interactively.
With arguments
<length>
: Password length (minimum 2 characters).<use_numbers>
:1
to include numbers,0
to exclude.<use_symbols>
:1
to include symbols,0
to exclude.
Help option
genpass --help
orgenpass -h
Examples
- Interactive mode (uses defaults)
genpass
- Generate a 16-character password with numbers and symbols
genpass 16
- Generate a 16-character password with numbers but no symbols
genpass 16 1 0
How it works
- The script starts with an alphabet set (
a-z
,A-Z
) and conditionally adds digits (0-9
) and symbols (!@#$%&()
). - The first and last characters are always alphabetic, selected randomly.
- Middle characters are generated using
/dev/urandom
, filtered withtr -dc
, and truncated withhead -c
. - All parts are combined into the final password and printed to stdout.
Input validation
- Password length must be a number ≥ 2.
- Number and symbol options must be either
0
or1
. - Any unrecognized arguments trigger an error message.
License
Free to use, modify, and share.