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

warpInLocations=("git@github.com:acc/toolkit.git" "git@github.com:acc/sms.git" "git@github.com:acc/boogle.git" "git@github.com: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 '13 at 14:54
  • each instance of the the warpInLocations – TheLegend May 24 '13 at 14:56
dir=$(basename git@github.com:acc/toolkit.git .git)

will set $dir to toolkit.

also useful is the dirname command.

| improve this answer | |

You need to proceed in two steps:

dir=${dir#*/}                       # Remove everything up to /
dir=${dir%.*}                       # Remove everything from the .
| improve this answer | |

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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