I have two arrays in a bash script say
ConfigA=("hostname1" "hostname2")
ConfigB=("hostname3" "hostname4")
Now I am passing an argument to the script specifying which Array I need to work with
For that I came up with a bash equivalent of ternary operator like below
int a = (b == 5) ? c : d;
equilvalent in bash
a=$([ "$b" == 5 ] && echo "$c" || echo "$d")
I tried to use this in my case as follows
env=$1
Config=$([ "$env" == "qa" ]] && echo "${ConfigA[@]}" || echo "${ConfigB[@]}")
However in the above case I get the output
hostname1 hostname2
but Its not an array, since when I try to loop over it, it print both at once.
I have also tried to surround the array within ()
like
Config=$([ "$env" == "qa" ]] && echo "( ${ConfigA[@]} )" || echo "( ${ConfigB[@]} )")
but that also doesn't help.
How do I assign the array to a new variable with a ternary operator?
I understand I can do it with a if-else
condition, but I would like to do it with ternary operator
Thanks for any help