Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created May 19, 2020 17:42
Show Gist options
  • Save Snarp/da259a0cc2a2c43998365351e2556237 to your computer and use it in GitHub Desktop.
Save Snarp/da259a0cc2a2c43998365351e2556237 to your computer and use it in GitHub Desktop.
Generate random string in Bash
#!/usr/bin/env bash
# How-to:
# bash rand_str.bash {{optional length}}
# Source: https://www.tldp.org/LDP/abs/html/string-manipulation.html
LEN=$1
if [ -z $LEN ]; then LEN=8; fi
str0="$$"
str1=$( echo "$str0" | md5sum | md5sum )
randstring="${str1:2:LEN}"
echo "random string: $randstring"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment