Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 6 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 '15 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

Summing up:

  • Get url without (optional) suffix:

  • Get repository name:

    reponame="$(basename "${url_without_suffix}")"
  • Get user (host) name afterwards:

    hostname="$(basename "${url_without_suffix%/${reponame}}")"
share|improve this answer

use regular expression: /([^/]+)\.git$/

share|improve this answer

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.