Tulsi Leathers
Tulsi Leathers

Reputation: 338

bash string incorrectly split

I have this script

#!/bin/bash
function clone {
    url=$(cli-tool "$1" that finds url)
    echo $url
    $(git clone ${url})
}

echo prints the correct url in the format "https://gitprovider.com/Example/_git/Repo%20Name" (not a real url but that mimics the real url)

But git clone outputs

fatal: could not create work tree dir 'Repo%20Name"': Invalid argument

If I execute

git clone "https://gitprovider.com/Example/_git/Repo%20Name"

the correct repo will be cloned.

So why isn't

$(git clone ${url})

Working?

Upvotes: 1

Views: 134

Answers (2)

chepner
chepner

Reputation: 531315

Command substitution is only needed when you want to use the output of a command as an argument to another command. In your case, the output of git clone is then parsed as sequence of words used to build a command line. You don't want to do that; you just want git clone ... to run and have its output displayed on the terminal.

Compare

 $ echo $(echo foo)
 foo
 $ $(echo foo)
 bash: foo: command not found

You just want git clone "$url", not $(git clone "$url").

Upvotes: 2

Rod Elias
Rod Elias

Reputation: 746

Instead of using $(git clone ${url}), just use git clone "${url}", i.e., drop the $( ) thing.

Upvotes: 1

Related Questions