15

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?

2
  • I don't have Chrome on my laptop here, but doing /c/Program\ Files\ (x86)/Mozilla\ Firefox/firefox.exe www.google.com brings up google ... in other words, you need to put the full path to the exe, and assuming Chrome works similar to firefox, you should be good Commented Mar 1, 2017 at 3:00
  • @Kotori My answer can be marked as correct answer which may help others too. Commented Mar 1, 2017 at 8:42

8 Answers 8

42

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

start chrome www.google.com 
Sign up to request clarification or add additional context in comments.

1 Comment

@Kotori This can be rated as correct answer so that it will be helpful for others too.
13
 start http://example.com 

Opens the default browser with the URL.

Tested on Git Bash/Windows 10.

Comments

3

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).

1 Comment

Quick question – is it correct that the quotes are not balanced with your escaping in the "you need to use" line? Should there not be an additional escaped quote? [Slightly related thought. It may also work if you use %3F instead of a literal question mark.]
3

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

1 Comment

Great answer. This works out of the box with Git Bash, no extra setup needed. And it can open local files too git web--browse my-local-file.html
1

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.

Comments

0

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

2 Comments

I'm getting only the message command not found even though I have the newest version for git bash
@Kotori, git is irrelevant here, that is a version control program. As far as the issue command not found try open /path/to/chrome www.site.com. or path/to/chrome www.site.com
0

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.

4 Comments

is this for cmd? or git bash?
UPDATE Somehow I successfully did 3. and chrome loads up but does not put up google.com right away. Is there any way to solve this?
**UPDATE I guess "chrome.exe <site address>" works for windows 10 and git bash. It is working
chrome.exe "example.com" should work as far as it is in your path. It should work from command prompt and bash both being in the path. You can check path in bash by echo $PATH and in command prompt with echo %PATH% and see if Google Chrome installation path is present there.
0
#!/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

Comments