-
-
Save manniru/a868fe56983399e65f1ea5393c618d71 to your computer and use it in GitHub Desktop.
Generate a random UUID in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Generate a pseudo UUID | |
uuid() | |
{ | |
local N B C='89ab' | |
for (( N=0; N < 16; ++N )) | |
do | |
B=$(( $RANDOM%256 )) | |
case $N in | |
6) | |
printf '4%x' $(( B%16 )) | |
;; | |
8) | |
printf '%c%x' ${C:$RANDOM%${#C}:1} $(( B%16 )) | |
;; | |
3 | 5 | 7 | 9) | |
printf '%02x-' $B | |
;; | |
*) | |
printf '%02x' $B | |
;; | |
esac | |
done | |
echo | |
} | |
if [ "$BASH_SOURCE" == "$0" ] | |
then | |
uuid | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment