In this article we will show you a bash script code to get random element from an array. This function will select an element randomly and return. It doesn’t use loops.
Code Example –
#!/bin/bash random_array_element() { local arr=("$@") printf '%s\n' "${arr[RANDOM % $#]}" } arr=(yo ho all together hoist the colors high) random_array_element "${arr[@]}" # Output: # all