cweiske
cweiske

Reputation: 31078

Passing config options via shell variable to git clone

I'm trying to pass dynamically created configuration options to git clone via an environment variable on bash.

Passing them directly works, but it does not work via the env variable:

$ git clone -c 'url.foo.insteadof=bar' git://git.cweiske.de/psist.git
... all fine

$ export PARAMS="-c 'url.foo.insteadof=bar'"; git clone $PARAMS git://git.cweiske.de/psist.git
error: invalid key: 'url.foo.insteadof

What can I do to make git recognize the options?

Upvotes: 2

Views: 884

Answers (2)

Manuel Barbe
Manuel Barbe

Reputation: 2164

You could use eval to get the parameters passed correctly.

export PARAMS="-c 'url.foo.insteadof=bar'"; 
eval git clone $PARAMS git://git.cweiske.de/psist.git

Upvotes: 2

l0b0
l0b0

Reputation: 58788

That's because in the first example the quotes are syntactic:

$ (set -o xtrace; git clone -c 'url.foo.insteadof=bar' git://invalid) 2>&1 | grep 'git clone'
+ git clone -c url.foo.insteadof=bar git://invalid

while in the second they are literal:

$ (set -o xtrace; export PARAMS="-c 'url.foo.insteadof=bar'" && git clone $PARAMS git://invalid) 2>&1 | grep 'git clone'
+ git clone -c ''\''url.foo.insteadof=bar'\''' git://invalid

You can use arrays to reliably pass arguments using variables:

$ (set -o xtrace; export PARAMS=('-c' 'url.foo.insteadof=bar') && git clone "${PARAMS[@]}" git://invalid) 2>&1 | grep 'git clone'
+ git clone -c url.foo.insteadof=bar git://invalid

Upvotes: 2

Related Questions