Skip to content
Home » Building a Custom Password Generator with Bash

Building a Custom Password Generator with Bash

Passwords are an essential part of our online lives. They protect our sensitive information and keep our accounts secure. But with so many different accounts and services to keep track of, it can be difficult to come up with unique and secure passwords for each one. That’s where a password generator script comes in. In this blog post, we’ll explore how to use a simple Bash script to generate strong, random passwords.

If you’re new to bash scripting then read this article first.

Generating a Random Password

To generate a random password, we’ll use the “openssl” command, which is a command-line tool for working with SSL/TLS certificates. We’ll use the “rand” command to generate a random string of characters.

#!/bin/bash

# This is a simple password generator script
password=$(openssl rand -base64 12)
echo $password

The “rand” command generates a string of random bytes, which we then encode using the “base64” option. The number “12” specifies the number of bytes to generate. In this case, we’re generating 12 bytes, which results in a password that is 16 characters long.

PassGen Alias

Alternatively you can create an alias in your .bashrc file and assign the following command to it, or just run it from the terminal:

echo $(openssl rand -base64 12)

Customizing the Password

While the above script will generate a random password, it may not meet all of your needs. For example, you may want to include uppercase letters, special characters, or a specific length.

#!/bin/bash

# This is a simple password generator script
# generate random password
password=$(openssl rand -base64 12)

# remove special characters
password=${password//[!a-zA-Z0-9]/}

# make first letter uppercase
password="$(tr '[:lower:]' '[:upper:]' <<< ${password:0:1})${password:1}"

# set a specific length of password
password=${password:0:10}

echo $password

In this example, we’ve added a few additional lines to the script to customize the password. The first line removes any special characters from the password. The second line makes the first letter of the password uppercase. And the third line sets the length of the password to 10 characters.

Using the Script

To use the script, you’ll need to make it executable. You can do this by running the “chmod +x” command on the script file.

chmod +x password_generator.sh

Once the script is executable, you can run it by typing the file name into the command line.

./password_generator.sh

This will generate a random password and print it to the screen.

Advanced Use-Cases

While the above script is a simple example of a password generator, there are many ways to customize and expand upon it. Here are a few ideas for advanced users:

  • Automating password generation: You can use the script in conjunction with other tools, such as a password manager, to automatically generate and save passwords for new accounts.
  • Creating a password generator web-app: You can use the script as the foundation for a web-based password generator. This would allow users to easily generate passwords from any device with a web browser.
  • Adding a user interface: You can add a command-line interface to the script, allowing users to specify options such as password length and character sets.
#!/bin/bash

# This is a simple password generator script

# Get user inputs for password length and character sets
echo "Enter password length (minimum 8 characters):"
read length
echo "Include special characters? (y/n):"
read special

#generate password
password=$(openssl rand -base64 $(( $length * 3 / 4)))

# check for special characters
if [ $special == "y" ]
then
    password=$(echo $password | tr -dc 'a-zA-Z0-9!#$%&()*+,-./:;<=>?@[\\]^_`{|}~')
fi

# set the specific length of the password
password=${password:0:$length}

echo $password

In this example, we’ve added a user interface that prompts the user to enter the desired password length and whether or not to include special characters. The script then uses these inputs to generate a password accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *

1 × 3 =