Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Shell Script Programming
  • Write a Linux Shell Script to Produce Passwords?

Write a Linux Shell Script to Produce Passwords?

April 20, 2018 / Dave Taylor / Linux Shell Script Programming / No Comments

I’ve become quite interested in the process of producing random passwords. I’d like to write my own Linux script to generate passwords! Can you offer me an example?

Congrats on wanting to write a program rather than find someone else’s solution already canned and ready to go. Of course, I’ll be showing you a script that does exactly what you want, but if I explain my thinking and logic, I bet you can take the concepts I outline and produce a new script that better fits your needs, so we’re good. 🙂

As with any program, the first and most important step is to think through the problem. Developers always talk about “divide and conquer” and it really is a critical part of any software development. In this instance, generating a complex and random password starts with the ability to generate a random character value. The pool or set of available characters can be enumerated, but how do you choose one at random?

Turns out that randomization is a big issue and there are computer science folk who spend a lot of time analyzing how random a random number generator truly is. Most end up having patterns if you look really closely, but for something of this nature, we should be okay with the built-in Linux shell $RANDOM feature. Never heard of it? It’s a “magic” variable that has a different – random – value every time you reference it!

Try this for yourself:

$ echo $RANDOM $RANDOM $RANDOM
25173 30854 3683

Turns out that the value it produces is between 1 and MAXINT (generally 2**16), so just about every time you see it used in a shell script, it’ll be with the modulus function: $(( $RANDOM % 100 )) produces a random number between 0-99, for example.

The other important piece of this particular puzzle is to know about the slick shell substring notation. If you’ve been struggling to get the first character of a string variable, or character #4, say, you’ll love this: ${variable:start:length} can be used anywhere you use a variable reference! First character? ${variable:0:1} does the trick.

Put these two together and you have most of what you need for a quick random password generator. Let’s just set the pool of available values to be all upper case, all lower case and all digits:

okay=”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″

Here’s the code I would write to solve this interesting problem:

length=10
ltrs=${#okay}
while [ $length -ge 0 ] ; do
  letter=”${okay:$RANDOM % $ltrs:1}”
  result=”$result$letter”
  length=$(( $length – 1 ))
done
echo “Result: $result”

A few quick invocations and we can see it’s working great:

$ lazy-passwords.sh
Result: qay4z3e2q8d
$ lazy-passwords.sh
Result: JaFIEz5zic3
$ lazy-passwords.sh
Result: kjHvyPqlgs9

Now it’s up to you to add punctuation, let users specify how long the password should be, etc. This should get you well on the road to success, however!

Pro Tip: Learning how to program shell scripts? I suggest you check out my best-selling book Wicked Cool Shell Scripts 2nd Ed for tons of fun scripts and also our Linux shell scripting help area for online tutorials too.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!
bash shell script, linux shell script, make passwords, password generation, password script, passwords, shell script

Leave a Reply Cancel reply

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

Search

Recent Posts

  • How Do I Update My Autofill Information in Brave?
  • Is It Possible to Save Voicemail Messages on an iPhone?
  • Automatically Sync Time and Show World Clock on my PC?
  • Rename and Add Artwork to Voice Memos Saved in Apple Music?
  • How Can I Use Google Lens within Google Chrome on a Chromebook?

On Our YouTube Channel

Casio PRO TREK Eco-Friendly PRW61-1A Chronograph Watch -- REVIEW

BENKS "MagClap" MagSafe External iPhone Battery -- TEST & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (195)
  • Apple iPad Help (144)
  • Apple Watch Help (52)
  • Articles, Tutorials, and Reviews (344)
  • Business Advice (199)
  • Chrome OS Help (23)
  • Computer & Internet Basics (758)
  • d) None of the Above (164)
  • Facebook Help (382)
  • Google, Chrome & Gmail Help (177)
  • HTML & Web Page Design (244)
  • Instagram Help (47)
  • iPhone & iOS Help (603)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (92)
  • LinkedIn Help (85)
  • Linux Help (162)
  • Linux Shell Script Programming (86)
  • Mac & MacOS Help (888)
  • Most Popular (16)
  • Outlook & Office 365 Help (24)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (17)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (912)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (45)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (56)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy