Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Last active February 1, 2021 14:42
Show Gist options
  • Save ruanbekker/834d6d1faee69ef225a347b5c8b4a5b7 to your computer and use it in GitHub Desktop.
Save ruanbekker/834d6d1faee69ef225a347b5c8b4a5b7 to your computer and use it in GitHub Desktop.
Bash: Return random number or string from array or range

Return random string from array:

#!/usr/bin/env bash
subnets[0]="subnet1"
subnets[1]="subnet2"
subnets[2]="subnet3"

size=${#subnets[@]}
index=$(($RANDOM % $size))
echo ${subnets[$index]}

Return random rumber from range:

#!/usr/bin/env bash
echo $[ $RANDOM % 3 + 1 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment