Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted
$ 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
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
add comment

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
add comment

Your Answer

 
discard

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.