Skip to content

Instantly share code, notes, and snippets.

@twfahey1
Created January 22, 2022 14:06
Show Gist options
  • Save twfahey1/40500860c7004a5df08e0ba6be942ba9 to your computer and use it in GitHub Desktop.
Save twfahey1/40500860c7004a5df08e0ba6be942ba9 to your computer and use it in GitHub Desktop.
Pure bash UUID generator
# Generate a pseudo UUID
uuid()
{
local N B T
for (( N=0; N < 16; ++N ))
do
B=$(( $RANDOM%255 ))
if (( N == 6 ))
then
printf '4%x' $(( B%15 ))
elif (( N == 8 ))
then
local C='89ab'
printf '%c%x' ${C:$(( $RANDOM%${#C} )):1} $(( B%15 ))
else
printf '%02x' $B
fi
for T in 3 5 7 9
do
if (( T == N ))
then
printf '-'
break
fi
done
done
echo
}
[ "$0" == "$BASH_SOURCE" ]
# Example: Create a string for an app that is spawning runners with a UUID in the name.
RUNNER_NAME="runner-$(uuid)"
echo "Creating $RUNNER_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment