如何在 git bash 终端(Windows 10)中打开谷歌浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42522100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:50:35  来源:igfitidea点击:

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

gitbashgoogle-chromewindows-10

提问by kost

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?

这将使谷歌浏览器窗口立即与网站一起弹出。有没有办法实现这一目标?

回答by vijayinani

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

这在 Windows 10 和使用 Git Bash 上对我有用。

start chrome www.google.com

回答by kost

 start http://example.com

Opens the default browser with the URL.

使用 URL 打开默认浏览器。

Tested on Git Bash/Windows 10.

在 Git Bash/Windows 10 上测试。

回答by quantme

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

这是我使用 Google Chrome从 Git bash(随Git for Windows提供)以隐身模式打开 URL 或文件的方法:

1. Open System Variables

1. 开放系统变量

  • Hit Windows Key+Rat the same time to get command prompt.
  • Then type sysdm.cpland hit the Enterkey.
  • 命中Windows Key+R在同一时间得到命令提示符。
  • 然后键入sysdm.cpl并按下Enter键。

run sysdm.cpl

运行 sysdm.cpl

  • Go to Advanced1and select Environmental Variables2
  • Select Path3from System variablesand click on Editbutton4.
  • 转到Advanced1并选择Environmental Variables2
  • 从中选择Path3System variables并单击Edit按钮4

Windows - Environment variables

Windows - 环境变量

Keep this window opened (while get thechrome.exefolder path).

保持此窗口打开(同时获取chrome.exe文件夹路径)。

Windows - Edit environment variable

Windows - 编辑环境变量

2. Get the chrome.exefolder path

2.获取chrome.exe文件夹路径

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 Chromeoption6and right-click on it and click on Properties7.

右键单击(或左键,如果您的鼠标是左撇子)Chrome 图标5您用来打开它的图标)并选择Properties7。通常此方法会打开更多 Chrome 操作,因此找到Google Chrome选项6并右键单击它并单击Properties7

Google Chrome shortcut - Properties

Google Chrome 快捷方式 - 属性

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

Shortcut8选项卡上,从Start in:9通常%programfiles(x86)%\Google\Chrome\Application)中选择文件夹路径

Google Chrome shortcut - Folder path

Google Chrome 快捷方式 - 文件夹路径

3. Adding Google Chrome folder path to Environment variables

3. 添加谷歌浏览器文件夹路径 Environment variables

Click on the Newbutton10from previously opened Edit environment variablewindow 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 OKbuttons from the opened windows.

单击先前打开的窗口中的New按钮10Edit environment variable,并将 Google Chrome 文件夹路径粘贴到新添加的第11行(如果路径开头或结尾存在双引号,请删除),然后单击OK打开的窗口中的所有按钮。

New environment variable

New environment variable

4. Add custom command to bash

4. bash 添加自定义命令

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

打开的Git的bash终端和编辑(或创建).bashrc与您喜欢的文本编辑器文件(在这个例子中Visual Studio代码

code ~/.bashrc

Git bash - code .bashrc

Git bash - code .bashrc

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

从下面附加代码。您可以根据需要重命名函数名称(chromeIt在本例中)。

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

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

如果您不想隐身模式,请删除该--incognito参数。

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.

非常重要的注意事项:验证您的行尾是否设置为 UNIX ( LF) 12。如果您不知道如何操作,请搜索google

Visual Studio code - Edit or create .bashrc

Visual Studio code - Edit or create .bashrc

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

保存.bashrc文件并重新加载它(使用 Git bash 终端):

source ~/.bashrc

Git bash - source .bashrc

Git bash - source .bashrc

or you can use the shorter versionof the command:

或者您可以使用该命令的较短版本

. ~/.bashrc

Git bash - source .bashrc shorter version

Git bash - source .bashrc shorter version

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

关闭 Git bash 终端窗口或输入exit并重新打开它。

Try to open a URL:

尝试打开一个 URL:

chromeIt google.com

Git bash - chromeIt open url

Git bash - chromeIt open url

or a file:

或文件:

chromeIt index.html

Git bash - chromeIt open file

Git bash - chromeIt open file

I hope it works for you.

我希望这个对你有用。

回答by PsychoX

You can use explorer.exethat can open URL in default browser:

您可以使用explorer.exe可以在默认浏览器中打开 URL:

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

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

因此有一个奇怪的错误: URL 不应包含?,因此:

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:

顺便说一下,我创建了 bash 函数来打开 Google 页面:

urlencode ()
{
    echo  | 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).

在 Windows 上的 Bash(Ubuntu 16.04.3 LTS;GNU bash,wersja 4.3.48)上测试。

回答by kalenpw

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

这应该可以解决问题。我无法在 Windows 10 上进行测试,但在 Ubuntu 中的 bash 上运行良好

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

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

回答by skjoshi

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 postexplains 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.
  1. 将谷歌浏览器的安装路径添加到您的系统路径或用户路径。之后,在您的命令提示符或 bash shell 中,您只需键入chrome "google.com". 对我来说是C:\Program Files (x86)\Google\Chrome\Application这篇文章解释了它。
  2. 在命令提示符中提供 chrome.exe 文件的完整路径。对于上述路径,命令将为"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" google.com.
  3. 另一种选择是您可以将 bash 中 exe 的完整路径别名为 chrome,然后使用它。

Please comment if you need more details.

如果您需要更多详细信息,请发表评论。