Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash: split string on delimiter and echo substring before/after
# http://tldp.org/LDP/abs/html/string-manipulation.html
function split_repo() {
local gitpath="${1:-github.com/mattmc3/myrepo}"
echo "\$gitpath = $gitpath"
echo "repo part \${gitpath##*/} = ${gitpath##*/}"
echo "user/repo \${gitpath#*/} = ${gitpath#*/}"
echo "url \${gitpath%%/*} = ${gitpath%%/*}"
echo "giturl/user \${gitpath%/*} = ${gitpath%/*}"
echo "user \${\${gitpath%/*}#*/} = ${${gitpath%/*}#*/}"
}
function split_repo2() {
local gitpath="$1"
local giturl="bbgithub.com"
local repo="${gitpath##*/}"
local usernm="${gitpath%/*}"
if [[ "$usernm" == */* ]]; then
giturl="${usernm%/*}"
usernm="${usernm#*/}"
fi
}
split_repo "bitbucket.org/joeuser/myrepo"
# split on the slash
value=start/end
echo start ${value%/*}
echo end ${value#*/}
# split on the colon
value=start:end
echo start ${value%:*}
echo end ${value#*:}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.