EN
Bash - clone git repository providing username and password directly when running (as command option or in URL)
9 points
In this short article, we would like to show how to clone git repository providing username and password when running using Bash.
Quick solution:
xxxxxxxxxx
1
git clone https://username:password@github.com/username/repository.git
Hint: do not forget to encode
username
andpassword
when they contains URLs special characters.
password
is optional in URL syntax, so by using:
xxxxxxxxxx
1
git clone https://username@github.com/username/repository.git
we will be asked to type password
while command is executed.
In practice username
and password
should be encoded. In this section you can find JavaScript's encodeURIComponent()
function equivalent.
xxxxxxxxxx
1
2
3
function create_utf8_code() {
4
local code="$(echo -n "$1" | xxd -ps)"
5
local length="${#code}"
6
local i
7
for (( i = 0; i < length; i += 2 ))
8
do
9
echo -n "%${code:$i:2}" | tr '[:lower:]' '[:upper:]'
10
done
11
}
12
13
function encode_uri_component() {
14
local text="${1}"
15
local length="${#text}"
16
local i char
17
for (( i = 0; i < length; ++i ))
18
do
19
char="${text:$i:1}"
20
[[ "$char" =~ [-_.!~*\'()a-zA-Z0-9] ]] && echo -n "$char" || create_utf8_code "$char"
21
done
22
}
23
24
25
encoded_username="$(encode_uri_component "username")"
26
encoded_password="$(encode_uri_component "P@$$word")"
27
28
git clone "https://$encoded_username:$encoded_password@github.com/username/repository.git"