bash

bash: pass multiple parameters from a single variable


In a bash call I want to put some constant parameters to a variable and don't lose StdOut & StdErr inside a pipe.

I have a call

git fetch origin "ref1:ref1" "ref2:ref2" "ref3:ref3"

Let's I'll put those constant values to a variable

fetch_refspec="'ref1:ref1' 'ref2:ref2' 'ref3:ref3'"

I see a solution to use a pipe, but I'm afraid to lose the output somehow. And I do not want to use files for a caching (tee command).

echo $refs | xargs git origin

I don't understand how to do this stuff cleverly. Or if it possible at all.
Later I want to put the output to a variable and analyze it.


Solution

  • Don't use a variable, use an array!

    declare -a gitArgs=("ref1:ref1" "ref2:ref2" "ref3:ref3")
    

    and pass it to the command you need,

    git origin "${gitArgs[@]}"