#!/bin/bash | |
# bash generate random alphanumeric string | |
# | |
# bash generate random 32 character alphanumeric string (upper and lowercase) and | |
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) | |
# bash generate random 32 character alphanumeric string (lowercase only) | |
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 | |
# Random numbers in a range, more randomly distributed than $RANDOM which is not | |
# very random in terms of distribution of numbers. | |
# bash generate random number between 0 and 9 | |
cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 1 | |
# bash generate random number between 0 and 99 | |
NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 2) | |
if [ "$NUMBER" == "" ]; then | |
NUMBER=0 | |
fi | |
# bash generate random number between 0 and 999 | |
NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 3) | |
if [ "$NUMBER" == "" ]; then | |
NUMBER=0 | |
fi |
This comment has been minimized.
This comment has been minimized.
If you need lowercase, tack another pipe and tr '[:upper:]' '[:lower:]' there. Thanks! |
This comment has been minimized.
This comment has been minimized.
Didn't google recently suffered downtime due to the lack of entropy ? Does reading from /dev/urandom ever block ? |
This comment has been minimized.
This comment has been minimized.
@javouhey There are many different problems with entropy, but in regards to using
|
This comment has been minimized.
This comment has been minimized.
@earthgecko Thank you for this script! Could you help me with an issue I have? When I run it it works like a charm, but when I put it in another script it hangs. Specifically I am running it a Composer post-install hook to generate a random salt. I suppose it starts a new child process for the command. In the Activity Monitor I could see the process is reaching nearly 100% CPU usage. |
This comment has been minimized.
This comment has been minimized.
My problem is most probably caused by: symfony/symfony#9409
|
This comment has been minimized.
This comment has been minimized.
Thanks for your script! I put this into my .bashrc random-string() { cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1 } Use it like this: $ random-string FWtnOOHAjbD6rNxWWEeVOCj7JXSEPGJQ $ random-string 16 CYmYZRlju4h4SN2l |
This comment has been minimized.
This comment has been minimized.
This doesn't work on a Mac: $ cat /dev/urandom | tr -dc 'a-zA-Z0-9' Nor on FreeBSD 9.2: $ cat /dev/urandom | tr -dc 'a-zA-Z0-9' |
This comment has been minimized.
This comment has been minimized.
Try this: |
This comment has been minimized.
This comment has been minimized.
This works on the CLI, but how is this used in the above script? I'm still getting errors within a shell script. |
This comment has been minimized.
This comment has been minimized.
Is there any advantage to using fold + head over just head?
|
This comment has been minimized.
This comment has been minimized.
@dlangille for mac use: |
This comment has been minimized.
This comment has been minimized.
What about these symbols !@#$%^&*()_+?><~`';][ etc. |
This comment has been minimized.
This comment has been minimized.
#!/bin/bash
NEW_UUID_MORE_CHARACTERS=$(cat /dev/urandom | tr -dc "a-zA-Z0-9!@#$%^&*()_+?><~\`;'" | fold -w 32 | head -n 1)
echo $NEW_UUID_MORE_CHARACTERS |
This comment has been minimized.
This comment has been minimized.
@mihigh |
This comment has been minimized.
This comment has been minimized.
what if i want my string of any particular length only let's just say , in this case , I want it of 10 characters long only ? |
This comment has been minimized.
This comment has been minimized.
@shahaksay94 fold it to 10 instead of 32 |
This comment has been minimized.
This comment has been minimized.
Nice. Thanks! |
This comment has been minimized.
This comment has been minimized.
# Without visual similar chars: IOl01
tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=32 count=1 status=none |
This comment has been minimized.
This comment has been minimized.
+1 @jmack for |
This comment has been minimized.
This comment has been minimized.
@dlangille, it works on FreeBSD if you are root. |
This comment has been minimized.
This comment has been minimized.
fewer random bytes could be discarded:
or to delete o, O, and 0, for example:
or as a function:
used like: |
This comment has been minimized.
This comment has been minimized.
Instead of base64 /dev/urandom | tr -d '/+' | dd bs=32 count=1 2>/dev/null |
This comment has been minimized.
This comment has been minimized.
Suggest, if I want Special character in the output of following command. strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 8 | tr -d '\n'; echo; |
This comment has been minimized.
This comment has been minimized.
@jordanmack +1. Stack overflow just rocks for this kind of thing: http://stackoverflow.com/questions/101362/how-do-you-generate-passwords |
This comment has been minimized.
This comment has been minimized.
for a full bunch of random chars: tr -dc [:graph:] < /dev/urandom | head -c 32 |
This comment has been minimized.
This comment has been minimized.
If you're going to generate just one password, fine, there's a million ways. But if you're going to generate a million, do you really want to discard 90% of the random bits your computer generates? It seems to me that the passwords will come out as a different kind of 'random'. That's why I suggest a different solution. |
This comment has been minimized.
This comment has been minimized.
on mac ``bash
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Soooo....what if I wanted to create a file of generated passwords using this script. Say 1000 of them. I know how to generate multiples, but is it possible to save them to a text file for later use? |
This comment has been minimized.
This comment has been minimized.
Why don't use
|
This comment has been minimized.
This comment has been minimized.
Instead of |
This comment has been minimized.
This comment has been minimized.
would be nice if everyone would stop catting all over themselves as the Here's an example that works on all POSIX systems even Sun unix and AIX and is always going to get 32 characters (ie a DWORD byte read 8 times):
|
This comment has been minimized.
This comment has been minimized.
@jkwages yes just append the What the Example:
An async threaded version would look like this:
|
This comment has been minimized.
This comment has been minimized.
Perl one-liner in case you only want to have one dependency: perl -e '@c=("A".."Z","a".."z",0..9);$p.=$c[rand(scalar @c)] for 1..32; print "$p\n"' Probably not as random as it could be, but should be good enough for this purpose. |
This comment has been minimized.
This comment has been minimized.
FYI:
This is on ISX Yosemite though I used the one-liner by @mckyj57, which works great. Thanks! |
This comment has been minimized.
This comment has been minimized.
IMHO here are some simpler options, pick the right tool for the job:
|
This comment has been minimized.
This comment has been minimized.
works for me on El Capitan but it triggers "set -e" for some reason and will stop scripts with set -e going; I've run the pieces of this and don't see any nonzero exit codes and am not sure why, but FYI.
I'm just using $RANDOM now as a workaround but would like a slightly larger space... |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Made an interactive version.
I don't particularly care for how I used echo to separate text bodies. I feel as if there's a more elegant way to do that. I'm actually pretty new to coding in general, so if you have any tips, please don't hesitate to give them. |
This comment has been minimized.
This comment has been minimized.
"simple is better then complex" PASSWORD=`pwgen -s 10 1` man pwgen - for more details |
This comment has been minimized.
This comment has been minimized.
These two commands generate random passwords and passphrases, respectively. You can use the first command with the right configuration file to generate random alphanumeric strings.
The password generator requires a file_with_characters containing all the characters you want the password to use, one character per line, and exactly one time each. The file must not contain blank lines, and lines must be newline-terminated. The passphrase generator requires a file_with_words containing all the words you want the passphrase to use, one word per line, and exactly one time each. The file must not contain blank lines, and lines must be newline-terminated. The --head-count option specifies the length of the password--in characters--or passphrase--in words. |
This comment has been minimized.
This comment has been minimized.
Very very useful script!!! But I have still question here: Is there any solution to handle this? |
This comment has been minimized.
This comment has been minimized.
@fufuwei try not using pipes .e.g.
|
This comment has been minimized.
This comment has been minimized.
@earthgecko cool! Thanks |
This comment has been minimized.
This comment has been minimized.
My latest incarnation:
Generates a 24-char string consisting of any printable ASCII character from |
This comment has been minimized.
This comment has been minimized.
Slight update to prevent password containing single quotes, making it easier to quote a variable containing the password.
|
This comment has been minimized.
This comment has been minimized.
This is what I ended up using in bash on OS X to have filename-safe string You get the idea |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
@fhfuwei re: pipefail setting, you can coax it to continue as so;
without || true at end, it will exit script. |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Fantastic work, thanks for sharing! |
This comment has been minimized.
This comment has been minimized.
Strange that this command after 100 iternations only produces a + symbol. What did I do wrong?
|
This comment has been minimized.
This comment has been minimized.
Good job! That's what I need! |
This comment has been minimized.
This comment has been minimized.
To generate WordPress security keys and salts for a # 64 random printable characters
# Excludes: space, double quote, single quote, and backslash
echo $(cat /dev/urandom | tr -dc [:print:] | tr -d '[:space:]\042\047\134' | fold -w 64 | head -n 1)
# jVig,+1&z3]}DT*$pvXPY#!z!^A-;[c0n!c*Ju=fy9`+yOauYAve<#fL]?>B9U;/ This will yield results similar to the official generator: https://api.wordpress.org/secret-key/1.1/salt/ |
This comment has been minimized.
This comment has been minimized.
Hi, How de generate code like this "YH7EF", Thanks |
This comment has been minimized.
This comment has been minimized.
Within a Docker container
|
This comment has been minimized.
This comment has been minimized.
@ManuCart #!/usr/bin/env bash
string=$(tr -dc '[:upper:]' < /dev/urandom | fold -w 5 | head -n 1)
if ((RANDOM % 2)); then
sed "s/./$((RANDOM % 10))/$((RANDOM % 5 + 1))" <<< "$string"
else
echo "$string"
fi This will generate string with length of 5 characters, sometimes with one number in it sometimes without. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I got it, how to do this |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
How can I create a random password with specific format.
|
This comment has been minimized.
This comment has been minimized.
Great, taking all the above:
|
This comment has been minimized.
This comment has been minimized.
Many Thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks! Exactly what i was searching for |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Just to throw my 2 cents: |
This comment has been minimized.
This comment has been minimized.
@x3ak, that is the best!!! |
This comment has been minimized.
This comment has been minimized.
I like
or
|
This comment has been minimized.
This comment has been minimized.
I use
which generates a 128-character alphanumeric string. |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
A shorter version to generate a random number in hexadecimal format: $ xxd -l16 -ps /dev/urandom Adjust the length (-l) to your liking. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
just use uuid:
|
This comment has been minimized.
This comment has been minimized.
Output examples:
|
This comment has been minimized.
This comment has been minimized.
This is useful. How do I create multiple lines of random characters of fixed length and store it to a file? I am doing it this way and it prints all my 5 lines onto a single line. |
This comment has been minimized.
This comment has been minimized.
Depending on the usecase, this might be sufficient and easy enough to write adhoc without adding another utility to your memory |
This comment has been minimized.
This comment has been minimized.
Another one in similar vein that ensures you won't get the same ID if you run in quick succession: |
This comment has been minimized.
This comment has been minimized.
Solution: env LC_CTYPE=C tr -dc "A-Za-z0-9_!@#\$%^&*()-+=" < /dev/urandom | head -c 32 | xargs |
This comment has been minimized.
This comment has been minimized.
A small error: you wrote that this generates lower case: |
This comment has been minimized.
This comment has been minimized.
I just had to generate some CSV with multiple random strings in single line. Total number of lines was 1,000,000,000. Bash wasn't able to serve me well. I even made it running in parallel on 30vcpu aws machine, but the IO was not saturated enough it was writing about 15-300MB/s. I had to implement generator in Java which was able to reach about 150mb/s, but I hit some other limitation from JVM perspective Random generator. It was the slowest part. But I was lucky and found this superfast random generator: Which helped me to saturate IO around ~400-500MB/s write, so my 500GB sample generated file took around 20-30minutes... Still, thanks for this gist. I just post where it finally led me after reaching some limits of unix tools |
This comment has been minimized.
This comment has been minimized.
Creates 10 passwords (15 char length) containing at least one character from each group: lower , upper case, digit. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
OSX: |
This comment has been minimized.
This comment has been minimized.
hangs when run |
This comment has been minimized.
This comment has been minimized.
I just made a simple function out of it. Also a small helper function to check for pwned passwords:
|
This comment has been minimized.
This comment has been minimized.
Eliminate cat:
|
This comment has been minimized.
This comment has been minimized.
awesome |
This comment has been minimized.
This comment has been minimized.
tr: Illegal byte sequence |
This comment has been minimized.
This comment has been minimized.
If anyone is getting the following from macOS:
Try this instead:
|
This comment has been minimized.
This comment has been minimized.
As @fnkr said, using
In this specific case I had to generate a random string with 4096 bytes programmatically. Using |
This comment has been minimized.
This comment has been minimized.
@Tagima using me@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
1WiaY4pBbvme@host:~$
me@host:~$ cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
SldlezjjelFEKScjS9SFEbdWoQeptU95
me@host:~$
me@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
oHMLHwUme@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
qdNQZtFGme@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
AtBByXkB7CKme@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
p12kCWu77me@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
9owjuxHme@host:~$ head -c 32 /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
j9yEZeDime@host:~$
|
This comment has been minimized.
This comment has been minimized.
@earthgecko Thank you! |
This comment has been minimized.
This comment has been minimized.
When I try to use it the command just doesn't exit. I assume it is because there is no EOF for cat to exit? |
This comment has been minimized.
This comment has been minimized.
use cat /dev/urandom | tr -dc 'a-za-z0-9' | fold -w 8 | head -n 1 for lowercase generator |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
Very nice and useful. |
This comment has been minimized.
This comment has been minimized.
This is a good script, but what about the random from 0.001 to 1? |
This comment has been minimized.
This comment has been minimized.
@icohigh - random float between 0 and 1 (to random precise between 1 and 3)
If you did not want random precise, e.g. 0.111, 0.123, 0.7, 0.356
|
This comment has been minimized.
This comment has been minimized.
Exactly what I need ;) |
This comment has been minimized.
This comment has been minimized.
Also a much more sketchy but works way head -c 1024 /dev/urandom | base64 | tr -cd "[:upper:][:digit:]" | head -c 32 tested on macOS 10.15.7 If you are curious, the chance of not generating 512 char and only have <32 upper&digit is 2.051*10^-195. and the chance of failing of 1024 is ....... ok, mathmetica literately tells me it "is too small to represent as a normalized machine number" and btw it takes 7ms to execute; on my machine |
This comment has been minimized.
This comment has been minimized.
Which one of all the different options given above is the fastest? |
This comment has been minimized.
Thanks :)