Bash function to generate a random alphanumeric string

Create a random string in Bash easily. You can use this Bash function in your .bashrc file to generate a random alphanumeric string. This comes in handy when you need to generate a long, secure password for example. Adjust to your needs.

# Generate a random 32 character alphanumeric string (upper 
# and lowercase) and numbers in Bash
random-string() {
        cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}

An example usage and output is

$ random-string
2fORqF7pau7bLafFuJbQNzK2D8yOLMnO

If needed you can add special characters to the tr command:

tr -dc '(\&\_a-zA-Z0-9\^\*\@'

This’ll generate a more secure random string, like:

$ random-string
w4LD528^K52@rrYx@vfEg1wKwRSMQXoP
$ random-string
BnhUn_ScM&sIs(Yn0d40PxJKUQN0QkHg
$ random-string
cFg3FRPHB6SXcQ(f8wGr7y1RKSR1i^&i

An example that works on all POSIX systems even Oracle Solaris and IBM AIX. It provides 32 random characters (ie a DWORD byte, or 32 bits), read 8 times):

tr -dc '[:alnum:]' < /dev/urandom | dd bs=4 count=8 2>/dev/null

This is kindly taken from ghost’s comment in earthgecko’s gist bash.generate.random.alphanumeric.string.sh. And denzuko writes:

would be nice if everyone would stop catting all over themselves as the < filename parameter does the same thing.

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):

tr -dc '[:alnum:]' < /dev/urandom | dd bs=4 count=8 2>/dev/null

These random strings generated in Bash make ideal passwords.

1 thought on “Bash function to generate a random alphanumeric string”

  1. .toolbelt-social-share{clear:both;font-size:1rem;display:grid;gap:calc(var(–toolbelt-spacing)/ 4);grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));margin-bottom:calc(var(–toolbelt-spacing) * 2);margin-top:calc(var(–toolbelt-spacing) * 2)}.toolbelt-social-share .toolbelt_share-api{display:none}.toolbelt-social-share a{padding:calc(var(–toolbelt-spacing)/ 4) var(–toolbelt-spacing);color:var(–toolbelt-color-light);align-items:center;display:flex;text-decoration:none}.toolbelt-social-share a:hover{color:var(–toolbelt-color-light)}.toolbelt-social-share a:hover span{text-decoration:underline}.toolbelt-social-share-api-enabled .toolbelt-social-share .toolbelt_share-api{display:inline}.toolbelt-social-share-api-enabled .toolbelt-social-share a{display:none}.toolbelt-social-share svg{-webkit-margin-end:calc(var(–toolbelt-spacing)/ 2);margin-inline-end:calc(var(–toolbelt-spacing)/ 2);height:1.5rem;width:1.5rem;vertical-align:middle}.toolbelt-social-share svg *{stroke:none;fill:currentColor}@media (min-width:500px){.toolbelt-social-share .toolbelt_whatsapp{display:none}}
    The other day I was searching for a particular email file in an Maildir on one of our mail servers. Knowing only the From address, I thought I’d use grep. Unfortunately it failed and gave me an error: /bin/grep: Argument list too long. Here is how to overcome this grep error…

    (adsbygoogle = window.adsbygoogle || []).push({});

    GNU Bash Logo

    /bin/grep: Argument list too long

    How to grep through a large number of files? For example:

    .wp-block-code {
    border: 0;
    padding: 0;
    }

    .wp-block-code > div {
    overflow: auto;
    }

    .shcb-language {
    border: 0;
    clip: rect(1px, 1px, 1px, 1px);
    -webkit-clip-path: inset(50%);
    clip-path: inset(50%);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    word-wrap: normal;
    word-break: normal;
    }

    .hljs {
    box-sizing: border-box;
    }

    .hljs.shcb-code-table {
    display: table;
    width: 100%;
    }

    .hljs.shcb-code-table > .shcb-loc {
    color: inherit;
    display: table-row;
    width: 100%;
    }

    .hljs.shcb-code-table .shcb-loc > span {
    display: table-cell;
    }

    .wp-block-code code.hljs:not(.shcb-wrap-lines) {
    white-space: pre;
    }

    .wp-block-code code.hljs.shcb-wrap-lines {
    white-space: pre-wrap;
    }

    .hljs.shcb-line-numbers {
    border-spacing: 0;
    counter-reset: line;
    }

    .hljs.shcb-line-numbers > .shcb-loc {
    counter-increment: line;
    }

    .hljs.shcb-line-numbers .shcb-loc > span {
    padding-left: 0.75em;
    }

    .hljs.shcb-line-numbers .shcb-loc::before {
    border-right: 1px solid #ddd;
    content: counter(line);
    display: table-cell;
    padding: 0 0.75em;
    text-align: right;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    white-space: nowrap;
    width: 1%;
    }
    grep -r "example.com" *
    -bash: /bin/grep: Argument list too long
    Code language: Bash (bash)

    Well, this was no wonder, since there are over 190500 files in that particular Maildir:

    ls -1 | wc -l
    190516
    Code language: Bash (bash)

    There are several ways of overcoming this error message. One, for instance is:

    find . -type f | xargs grep "example.com"Code language: Bash (bash)

    A second option to overcome the error is: substitute the “*” (asterisk) with a “.” (dot), like:

    grep -r "example.com" .Code language: Bash (bash)

    In newer versions of grep you can omit the “.“, as the current directory is implied.

    Protip: did you know you can use grep on Windows? This comes in handy for forensic log parsing and analysis on Windows Server IIS.

    Argument list too long when deleting files in Bash (Bonus!)

    If you’re trying to delete a lot of files in Bash, this may also fail with an “Argument list too long” error message:

    ~/rm-test$ rm *
    -bash: /bin/rm: Argument list too long
    Code language: Bash (bash)

    “Argument list too long” indicates when a user feeds too many arguments into a single command which hits the ARG_MAX limit. The ARG_MAX defines the maximum length of arguments to the exec function. Other commands subject to this limitation are commands such as rm, cp, ls, mv, and so on. (thanks Hayden James).

    Generate a random alphanumeric string in Bash

    There are a couple of command you can use to resolve this error and to remove those many thousands files. In my following examples, I have a dir ~/rm-test with 200.000 files, that I’ll use (and I’ll time the commands for speed measurement).

    time find . -type f -delete

    real 0m2.484s
    user 0m0.120s
    sys 0m2.341s
    Code language: Bash (bash)

    time ls -1 | xargs rm -f

    real 0m2.988s
    user 0m0.724s
    sys 0m2.375s
    Code language: Bash (bash)

    mkdir empty
    time rsync -a --delete empty/ rm-test/

    real 0m2.880s
    user 0m0.170s
    sys 0m2.642s
    Code language: Bash (bash)

    Or remove the entire directory:

    time rm -r rm-test/

    real 0m2.776s
    user 0m0.220s
    sys 0m2.510s
    Code language: Bash (bash)

    You can recreate that directory with the mkdir command if you still need it. Create 200.000 small test file with: dd if=/dev/urandom bs=1024 count=200000 | split -a 4 -b 1k - file.

    As you can see, the timing results don’t differ that much. In this test, using find with -delete is the fastest solution, followed by rm -Rf. The slowest is using ls-1 | xargs rm -f. The downside of rsync is that 1) it is CPU heavy, and 2) you need to create an empty directory first.
    Related PostsSsh shorthand name for hostnamesRedirect HTTP to HTTPSCheck IP address blacklist status in BashInstall Elasticsearch on CentOS 6.7Share this:Click to share on Twitter (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on Pocket (Opens in new window)Click to share on LinkedIn (Opens in new window)Click to share on WhatsApp (Opens in new window)Click to share on Reddit (Opens in new window)Click to share on Tumblr (Opens in new window)Click to share on Pinterest (Opens in new window)Click to share on Telegram (Opens in new window)Click to share on Skype (Opens in new window)Click to email this to a friend (Opens in new window)Click to print (Opens in new window)var toolbelt_social_share_description = “The other day I was searching for a particular email file in an Maildir on one of our mail servers. Knowing only the From address, I…”;Share Tweet this
    Share this
    Share this
    Save this
    Share this

Comments are closed.