I am writing my first bash script. I am making it install all the repos I have on GitHub.

warpInLocations=("[email protected]:acc/toolkit.git" "[email protected]:acc/sms.git" "[email protected]:acc/boogle.git" "[email protected]:acc/cairo.git")

Those are them when I install them.

echo "warping in toolkit, sms, boogle and cairo"
for repo in "${warpInLocations[@]}"
  warpInDir=$(echo ${warpToLocation}${repo} | cut -d'.' -f1)
  if [ -d "$warpToLocation"]; then
    echo "somethings in the way.. $warpInDir all ready exists"
    git clone $repo $warpInDir


This line here, I wanted it to give me a folder named toolkit or sms, so after the / and before the . in the warp in locations, but it's selecting git@github instead. I guess, because it's after the ..

How can I get it to select the name in the repo?

  • That's a bit confusing. What do you want? The string between the last / and the following .?
    – Uwe
    May 24, 2013 at 14:54
  • each instance of the the warpInLocations
    – TheLegend
    May 24, 2013 at 14:56

3 Answers 3

dir=$(basename [email protected]:acc/toolkit.git .git)

will set $dir to toolkit.

also useful is the dirname command.


You need to proceed in two steps:

[email protected]:acc/toolkit.git
dir=${dir#*/}                       # Remove everything up to /
dir=${dir%.*}                       # Remove everything from the .

in bash, you can also use a regular expression and capturing parentheses

for repo in "${warpInLocations[@]}"; do
    [[ $repo =~ /([^.]+)\. ]] && dir=${BASH_REMATCH[1]}
    # ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .