1

I have a script that runs another bash script with N optional parameters, such as:

./my_script.sh --param_1 <value> --param_2 <value> ... --param_N <value>

In my main script, i would like to have N arrays with possible values for each of the N parameters, such as:

PARAM_1_VALS=(1 10 50)
PARAM_2_VALS=(50 10 20)
PARAM_N_VALS=("a" "b" "c")

I'd like to know if there is any function that, given an array of N arrays, returns all the possible combinations among those arrays (ex: (1 50 "a"), (1 50 "b"), ..., (50 20 "c"))

The desired script would be:

all_combinations=get_all_combinations(params_array)
for combination in ${all_combinations[@]}; do
   #execute my_script.sh with combination
done

My current solution is performing badly since i'm just iterating through all arrays in nested loops to get all values. I would like to remove the nested loops and look for a better approach.

ps: what i'm looking is very similar to python's itertools.combinations() solution

Thanks

2
  • If you know python, then write it in python. shell is the wrong language for complex data processing, almost any other language would be better. If you don't know python, then learn enough to do what you need. Even learning it from scratch will save you time (and compute cycles)....same applies to any other language.
    – cas
    Commented Aug 23, 2021 at 15:45
  • great advice. Thank you! Commented Aug 23, 2021 at 20:52

1 Answer 1

2

You can use brace expansion, but it only works on literal values, i.e. you can't use variables inside the braces.

for c in --param1\ {1,10,50}\ --param2\ {50,10,20}\ --param3\ {a,b,c} ; do
    echo "$c"
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .