Open a piped URL in Google Chrome in Bash (OSX)

2020-03-26 bash google-chrome sed

I'm trying to pass a value (Git's origin URL, manipulated with sed) via pipe to Chrome in bash on OSX. I have tried the following:

I can get the two separate parts working, but when I put them together Chrome does not open the URL as I would expect.

  • git config --get remote.origin.url | sed "s/[email protected]/https:\/\//g" (outputs https://github.com/myProject/)

  • echo 'https://example.com' | /usr/bin/open -a "/Applications/Google Chrome.app" (correctly opens https://example.com)

However, if I stick these two together, Chrome opens but just to a blank page (about:blank):

  • git config --get remote.origin.url | sed "s/[email protected]/https:\/\//g" | xargs /usr/bin/open -a "/Applications/Google Chrome.app"
  • or
  • git config --get remote.origin.url | sed "s/[email protected]/https:\/\//g" | /usr/bin/open -a "/Applications/Google Chrome.app"

Answers

On my machine I found that I had to do

git config --get remote.origin.url | sed 's/\.git//; s/[email protected]/www\./; s#https://#www\.#; s#\.com:#\.com/#'

In order to get the output

www.github.com/myProject/

This is the URL format that chrome seems to like.

  1. I added 's/\.git//; to get rid of the .git at the end of the URL
  2. I added s/[email protected]/www\./; s#https://#www\.#; to swap [email protected](For ssh cloned repos) and https://(For non-ssh cloned repos) with www.
  3. I added s#\.com:#\.com/# to get rid of the colon ':'

And here is the whole shebang

git config --get remote.origin.url | sed 's/\.git//; s/[email protected]/www\./; s#https://#www\.#; s#\.com:#\.com/#' | xargs /usr/bin/open -a "/Applications/Google Chrome.app"

Related