0

So, I am trying to create an alias to open the browser in a specific URL for me when I run the pr command on my command line.

function pr() {
  repoName=(basename `git rev-parse --show-toplevel`)
  branchName=(`git rev-parse --abbrev-ref HEAD`)
  $repoName <- printing a-public
  $branchName <- printing acq-248
  open -a "Google Chrome" "https://bitbucket.org/company/$repoName/pull-requests/new?source=$branchName&event_source=branch_list"
}

So, when I run pr, I can see the $repoName and the $branchName has the right value, but the opened url does interprets it partially -> https://bitbucket.org/company/Users/v/projs/a-public/pull-requests/new?source=acq-248&event_source=branch_list

So, what would be the right way of passing these variables to concatenate into the URL I wanna open ?

1
  • Why are you tagging this question as zsh and sh and shell, if you are interested in a bash solution only? Commented Mar 5, 2020 at 8:33

1 Answer 1

1

both of them are an array assignment.

repoName=(basename `git rev-parse --show-toplevel`)
branchName=(`git rev-parse --abbrev-ref HEAD`)

If you want to expand the command output in a variable use command substitution without the backticks but the $( )

repoName=$(basename $(git rev-parse --show-toplevel))
branchName=$(git rev-parse --abbrev-ref HEAD)

Try quoting the url with the %q so it can be pass to the shell as a safe argument.

var="https://bitbucket.org/company/$repoName/pull-requests/new?source=$branchName&event_source=branch_list"

Quote it shell safe.

printf -v url '%q' "$var"

Now try your command.

open -a "Google Chrome" "$url"
Sign up to request clarification or add additional context in comments.

8 Comments

You killed it man, tks. I didn't know about $() thing. working smoothly!
Glad to be of help.
You should quote the inner command substitution and use lowercase variable names. To make the variables local to the function, use the declare or local keyword: declare repo=$(basename "$(git rev-parse --show-toplevel)")
@Freddy, right declare the variables local but without the assignment.
|

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.