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.

1
  • 1
    Use array as fetch_refspec="(ref1:ref1' 'ref2:ref2' 'ref3:ref3'); git fetch origin "${fetch_refspec[@]}" Commented Sep 18, 2017 at 11:31

1 Answer 1

9

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[@]}"
Sign up to request clarification or add additional context in comments.

2 Comments

Got harsh bugs :) with Array variables may not (yet) be exported. But OK with it. Forced to use source command.
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 ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.