简体   繁体   中英

Shell script for git clone doesn't work with a variable as password

I'm getting trouble with a shell script for git, this script will ask the password for a robotic user account to make a clone/push like this:

echo 'write the pass'
read pass
git clone https:\\user:$pass@bitbucket... destination

The problem is that it shows me: fatal: Authentication failed for 'https:\\user:$pass@bitbucket...'

I tried with https:\\user:${pass}@bitbucket... and same result

If I try directly without any variable like git clone https:\\user:password@bitbucket... destination it works perfect.

Do you guy know what's the problem?

You have a couple issues here. One is that you're using backslashes instead of forward slashes. That's likely to be interpreted poorly by the shell.

Another issue is the quoting problem: you may have a space in the password, and if so, the shell will interpret it as two arguments.

Finally, if the password you're using contains certain characters, you'll need to escape them for the password to work correctly. For example, some passwords contain an equal sign, which must be encoded as %3D . There isn't a good way to do this in shell, but if you have Perl installed (which you likely do with Git), you can change this to the following:

echo 'write the pass'
read pass
pass="$(echo "$pass" | perl -pe 's/([^A-Za-z0-9._~-])/sprintf "%%%02X", ord($1)/ge')"
git clone https://user:$pass@bitbucket.org/...

Since a percent-encoded password will never contain a space, it's no longer necessary to quote the URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM