I didn't process that it was in an array and was thinking whitespace-separated in a string. This solution will work with that, but given that it's an array, go with manatwork's solution (
This isn't too bad with
sed and a subshell. Just how easy the regex is depends on what you can guarantee about the options. If the options are all one "word" (
a-zA-Z0-9 only), then a simple beginning word boundary (
\<) will suffice:
command $(echo $my_array | sed 's/\</-/g') "$1"
If your options have other characters (most likely
-), you'll need something a bit more complex:
command $(echo $my_array | sed 's/\(^\|[ \t]\)\</\1-/g') "$1"
^ matches the beginning of the line,
[ \t] matches a space or tab,
\| matches either side (
\) groups (for the
\|) and stores the result,
\< matches the start of a word.
\1 begins the replacement by keeping the first match from the parens (
- of course adds the dash we need.
These work with gnu sed, if they don't work with yours, let me know.
And if you'll be using the same thing multiple times, you may want to just calculate it once and store it:
opts="$(echo $my_array | sed 's/\(^\|[ \t]\)\</\1-/g')"
command $opts "$1"
command $opts "$2"