4

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.

it3xl
  • 2,372
  • 27
  • 37
  • 1
    Use array as `fetch_refspec="(ref1:ref1' 'ref2:ref2' 'ref3:ref3'); git fetch origin "${fetch_refspec[@]}"` – anubhava Sep 18 '17 at 11:31

1 Answers1

8

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[@]}"
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    Got harsh bugs :) with [Array variables may not (yet) be exported](https://stackoverflow.com/questions/5564418/exporting-an-array-in-bash-script/5564589#5564589). But OK with it. Forced to use `source` command. – it3xl Sep 19 '17 at 08:40
  • On the other hand, using of the `source` command to call subscripts kills preconditions. I.e. the shortest form `if [[ ... ]]; then exit` at beginning of subscripts now must be converted and wraped over entire script. And I have many preconditions )) – it3xl Sep 19 '17 at 08:59