1

I have the variables below in a large and important SH file and I need to remove some data from a variable and keep only part of the text.

I get "repoTest" with a link to an internal git repo and I need the variable "nameAppTest" to only receive the constant data after the last "/".

Example:

I get: repoTest="ssh://[email protected]/code/ecsb/name-repo.git"

I try to do a split: nameAppTest=$(echo "$repoTest"|cut -d'/' -f5|sed -e 's/.git//g')

Response I get: echo "$nameAppTest" (ecsb).

What I expect to receive: name-repo

I tried like this and failed: nameAppTest=$(echo "$repoTest"|cut -d'/' -f5|sed -e 's/.git//g')

1 Answer 1

2

Here's a nifty trick:

nameAppTest=$(basename "$repoTest" .git)

Uses basename to get just the last component of the URL, and strip the extension all in one step.

You can also use sh parameter expansion to do it in two steps without any external programs:

# Remove everything up to and including the last /
temp="${repoTest##*/}"
# Remove the trailing .git
nameAppTest="${temp%.git}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.