Generate a random string in bash

Generating random string in used in cryptography and it is very important in our day to day programming to generate a random password generation, random username access or login .. etc., To generate a random string with other characters you can add those characters to array1.

 #!/bin/bash

MAXSIZE=8

array1=(
w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D
F H J K L Z X C V B N M 2 3 4 7 8 1 5 6 9
)

MODNUM=${#array1[*]}

pwd_len=0

while [ $pwd_len -lt $MAXSIZE ]
do
  index=$(($RANDOM%$MODNUM))
  password="${password}${array1[$index]}"
  ((pwd_len++))
done
echo $password

Advertisements

3 thoughts on “Generate a random string in bash

  1. Hi
    Nice script. Why did you select exactly those characters? Uppercase “o” is hard to distinguish from zero, but why is i.e. uppercase “s” missing?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s