user7638737
user7638737

Reputation:

how to open google chrome in git bash terminal (Windows 10)

I want to make a command like:

chrome "site.com"

which will make a google chrome window to pop up with the site instantly. Is there any way to achieve this?

Upvotes: 15

Views: 26002

Answers (8)

SftPlnIT
SftPlnIT

Reputation: 1

#!/bin/bash

baseUrl='https://www.google.com/search?q='
query='windows bash start browser'
url="$baseUrl""$query"

start chrome "$url"
start firefox "$url"
start msedge "$url"

#Default browser
start "" "$url"

This works for me to open an URL in the default browser on Windows 11 in git bash (GNU bash, version 5.2.26)

Without the "", passing e.g. an URL containing spaces would open cmd.exe, with the URL as the window title. (Spaces are not allowed in an URL, but it works in Firefox, Edge and Chrome.)

If the first parameter has double quotes it uses that as the optional TITLE for the new window... give it an empty title
https://stackoverflow.com/a/154090

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/start

Upvotes: 0

broc.seib
broc.seib

Reputation: 22731

Depending on your situation, a more cross-platform solution might be to use git web--browse, which will attempt to open a url (or file) in a browser.

For example, this would open a link to github for opening a PR:

URL="${GIT_REPO}/compare/${CURRENT_BRANCH}?expand=1"
echo "opening $URL"
git web--browse $URL

There is also a --browser option where you can target a specific browser.

See: https://git-scm.com/docs/git-web--browse

Upvotes: 2

kost
kost

Reputation: 730

 start http://example.com

Opens the default browser with the URL.

Tested on Git Bash/Windows 10.

Upvotes: 13

AgainPsychoX
AgainPsychoX

Reputation: 1791

You can use explorer.exe that can open URL in default browser:

explorer.exe "https://www.google.com/"

Therefore there is a weird bug: URL should not contain ?, so:

explorer.exe "https://www.google.com/search?q=foo+bar"

will fail, and you need to use:

explorer.exe "https://www.google.com/search?q=foo+bar&\""

to work around.

By the way, I created bash function to open up Google page:

urlencode ()
{
    echo $1 | sed -e 's:%:%25:g' -e 's: :%20:g' -e 's:<:%3C:g' -e 's:\[:%5B:g' \
                  -e 's:>:%3E:g' -e 's:#:%23:g' -e 's:{:%7B:g' -e 's:\*:%2A:g' \
                  -e 's:}:%7D:g' -e 's:|:%7C:g' -e 's:+:%2B:g' -e 's:\\:%5C:g' \
                  -e 's:/:%2F:g' -e 's:?:%3F:g' -e 's^:^%3A^g' -e 's:\!:%21:g' \
                  -e 's:@:%40:g' -e 's:=:%3D:g' -e 's:&:%26:g' -e 's:\$:%24:g' \
                  -e 's:;:%3B:g' -e 's:~:%7E:g' -e 's:`:%60:g' -e 's:\^:%5E:g' -e 's:\]:%5D:g' 
}

google ()
{
    local a="$(urlencode "$(echo "$@")")"; a="${a// /+}"; 
    explorer.exe "https://www.google.com/search?q=${a// /+}&\""
}

alias ggle='google'
alias g='google'

You can use it as follows:

 g site:cppreference.com c++ empty string view

I find it quite useful, especially while coding ;)

Tested on Bash on Windows (Ubuntu 16.04.3 LTS; GNU bash, wersja 4.3.48).

Upvotes: 3

quantme
quantme

Reputation: 3657

Here's what I'm using to open a URL or a file with Google Chrome into incognito mode from Git bash (provided with Git for Windows):

1. Open System Variables

  • Hit Windows Key+R at the same time to get command prompt.
  • Then type sysdm.cpl and hit the Enter key.

run sysdm.cpl

  • Go to Advanced1 and select Environmental Variables2
  • Select Path3 from System variables and click on Edit button4.

Windows - Environment variables

Keep this window opened (while get the chrome.exe folder path).

Windows - Edit environment variable

2. Get the chrome.exe folder path

Right click (or left if you have your mouse as left-handed) the Chrome icon5 (the one you use to open it) and select Properties7. Usually this method opens more Chrome actions so locate Google Chrome option6 and right-click on it and click on Properties7.

Google Chrome shortcut - Properties

On the Shortcut8 tab select the folder path from Start in:9 (Commonly %programfiles(x86)%\Google\Chrome\Application)

Google Chrome shortcut - Folder path

3. Adding Google Chrome folder path to Environment variables

Click on the New button10 from previously opened Edit environment variable window and paste the Google Chrome folder path into the new added line11 (delete the double quotes if exist at the beginning or the end of the path) and click on all the OK buttons from the opened windows.

New environment variable

4. Add custom command to bash

Open the Git bash terminal and edit (or create) the .bashrc file with your prefered text editor (Visual Studio code in this example)

code ~/.bashrc

Git bash - code .bashrc

Append the code from below. You can rename the function name (chromeIt in this example) to your needs.

# Open google Chrome
function chromeIt {
    if [ -z "$1" ]; then
        # display usage if no parameters given
        echo "Usage: chromeIt <file_name>|<url>"
    else
        if [ -f "$1" ] ; then
            chrome --incognito $(pwd)/$1
        else
            chrome --incognito $1
        fi
    fi
}

If you don't want incognito mode remove the --incognito parameter.

Very important note: Verify that your line ending is set as UNIX (LF)12. If you don't know how to do it search into google.

Visual Studio code - Edit or create .bashrc

Save the .bashrc file and reload it (using the Git bash terminal):

source ~/.bashrc

Git bash - source .bashrc

or you can use the shorter version of the command:

. ~/.bashrc

Git bash - source .bashrc shorter version

Close the Git bash terminal window or enter exit and re-open it.

Try to open a URL:

chromeIt google.com

Git bash - chromeIt open url

or a file:

chromeIt index.html

Git bash - chromeIt open file

I hope it works for you.

Upvotes: 1

skjoshi
skjoshi

Reputation: 2533

You have few options here:

  1. Add the installation path of Google Chrome to your system path or user path. After that in your command prompt or bash shell, you can just type chrome "google.com". For me it is C:\Program Files (x86)\Google\Chrome\Application. This post explains it.
  2. Provide complete path for the chrome.exe file in your command prompt. For the above mentioned path, the command will be "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" google.com.
  3. One more option will be you can alias the complete path of exe in bash as chrome and then use it.

Please comment if you need more details.

Upvotes: 0

vijayinani
vijayinani

Reputation: 2634

This worked for me on Windows 10 and using Git Bash.

start chrome www.google.com

Upvotes: 43

kalenpw
kalenpw

Reputation: 695

This should do the trick. I can't test on Windows 10 but works fine on bash in Ubuntu

google-chrome http://www.google.com

Upvotes: 0

Related Questions