Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • Videos
  • Most Popular
  • Top Categories
  • Books
  • About Dave
  • Ask Me
  • > Donate <
  • 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

  • Can I Have Microsoft Edge Suggest Strong Passwords?
  • How Can I Password Protect Tax Forms Sent Via Email?
  • Easy Way to Add Emoji to Tweets from a Web Browser?
  • How Do You Post Multiple Photos in Instagram?
  • How Can I Change the Default Search Engine in Google Chrome to Microsoft Bing?

On Our YouTube Channel

DIGDIY BUDGET ANC TWS Earbuds [D10WH] -- REVIEW

Rush Charge Hinged Smartphone Battery Stand -- UNBOXING & REVIEW

Categories

  • AdSense, AdWords and PPC Help (106)
  • Amazon Echo & Kindle Help (79)
  • Amazon, eBay and Online Shopping Help (157)
  • Android Help (158)
  • Apple Watch Help (49)
  • Articles, Tutorials and Reviews (326)
  • Business Advice (191)
  • Computer and Internet Basics (714)
  • d) None of the Above (160)
  • Facebook Help (364)
  • Google & Gmail Help (154)
  • HTML and Web Design (243)
  • Instagram Help (43)
  • iPad Help (136)
  • iPhone Help (570)
  • LinkedIn Help (79)
  • Linux Help (147)
  • Linux Shell Script Programming (86)
  • Mac & MacOS Help (830)
  • Most Popular (10)
  • MP3 Player Help (181)
  • Outlook & Office 365 Help (9)
  • PayPal Help (68)
  • Pinterest Help (53)
  • Reddit Help (11)
  • SEO & Marketing (80)
  • Spam, Scams & Security (82)
  • Trade Show Updates (23)
  • Twitter Help (212)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows 10 Help (825)
  • Wordpress Help (201)
  • Writing and Publishing (72)
  • YouTube Help (43)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (40)

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."
© 2020 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy