Run a command using arguments that come from an array

Giving the arguments from an array is easy, "${array[@]}" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t flags as we go:

#!/bin/bash
tabs=("first tab" "second tab")
args=()
for t in "${tabs[@]}" ; do 
    args+=(-t "$t")
done
app "${args[@]}"

Use "$@" instead of "${tabs[@]}" to take the command line arguments of the script instead of a hard coded list.


tabs=("-t" "one tab" "-t" "second tab")
echo app "${tabs[@]}"
app -t one tab -t second tab

So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.

Tags:

Bash