DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

How can I generate a random string in a bash script, to be used as a password?


2 Answers

+3 votes
Best answer
pwgen --secure --symbols 100 1
selected by

Actually I would recommend this solution over mine, because it uses a program which does exactly this and can be configured more easily.

+1 vote

Well, since you can use Ruby in a Bash script, here is a Ruby one-liner for ya ;)

LENGTH=16 ruby -e 'ENV["LENGTH"].to_i.times { print Random.rand(33..126).chr.to_s }; puts'

To change the set of characters you want to use, just modify the ASCII codes in the rand(..) function.


Why 33..126?


Because those are the printable ASCII characters. Of course, you could use Extended ASCII or UTF-8 but some websites actually only want you to use basic ASCII characters.

Contributions licensed under CC0