Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Given ANY GitHub repository url string like:




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.


share|improve this question

2 Answers 2

up vote 5 down vote accepted
$ url=git://
$ basename=$(basename $url)
$ echo $basename
$ filename=${basename%.*}
$ echo $filename
$ extension=${basename##*.}
$ echo $extension
share|improve this answer
Thanks, trying to make it a one liner, but not working. REPO_NAME=${`basename $REPO_URL`%.*} –  Justin Aug 14 '12 at 4:17
echo $(basename "$url" ".${url##*.}"). –  quanta Aug 14 '12 at 6:31
+1. Is there anything similar to get the hostname, i.e., instead, @quanta? –  chepukha Mar 31 at 2:19

I'd go with basename $URL .git.

share|improve this answer
This returns my-repo.git, only want my-repo. Thanks. –  Justin Aug 14 '12 at 4:02
Answer adjusted. –  womble Aug 14 '12 at 4:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.