I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001
, test002
, test003
and test004
.
If I set it like normal I'd obviously do it like this:
test=test001
But I want it to choose a random one from the for strings I have. I know I could do something like this, which I have done previously, but that was when choosing a random file from a directory:
test="$(printf "%s\n" "${teststrings[RANDOM % ${#teststrings[@]}]}")"
But in this case I am not sure how to set testrings
.
teststrings=( test001 test002 test003 test004 )
, then your code will work. To save running in a subshell,printf -v test "%s\n" "${teststrings[RANDOM % ${#teststrings[@]}]}"