Bash – Extract repository name from GitHub url in bash

bashgithubregexregular expressionsstrings

Given ANY GitHub repository url string like:

git://github.com/some-user/my-repo.git

or

git@github.com:some-user/my-repo.git

or

https://github.com/some-user/my-repo.git

What is the best way in bash to extract the repository name my-repo from any of the following strings? The solution MUST work for all types of urls specified above.

Thanks.

Best Answer

$ url=git://github.com/some-user/my-repo.git
$ basename=$(basename $url)
$ echo $basename
my-repo.git
$ filename=${basename%.*}
$ echo $filename
my-repo
$ extension=${basename##*.}
$ echo $extension
git
Related Topic