我刚刚为Windows安装了Git,很高兴看到它安装了Bash。

我想以同样的方式自定义shell,我可以在Linux下(例如设置别名ll为ls -l),但我似乎找不到.bashrc或等效的配置文件。

我应该编辑什么?


当前回答

访问:C:\Program Files\Git\etc\profile.你在里面找到你需要的一切。 或者在您的主目录中创建.bashrc和.bash_profile。

. bash_profile

# generated by Git for windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

. bashrc

echo "Hello World!"

其他回答

如果您使用的是Git for Windows 2.21.0或更高版本(在撰写本文时),则应该在主目录中编辑.bash_profile。

你可以直接将.bash_profile指向.bashrc,但是如果你的.bash_profile发生了什么,那么就不清楚为什么你的.bashrc不能工作。

我把我所有的别名和其他环境的东西放在.bash_profile中,我还添加了这一行:

echo "Sourcing ~/.bash_profile - this version of Git Bash doesn't use .bashrc"

然后,在。bashrc中

echo "This version of Git Bash doesn't use .bashrc. Use .bash_profile instead"

(基于@harsel的回应。我本想发表评论,但我现在还没有观点。)

我必须添加一个用户环境变量,HOME, C:\Users\<你的用户名>,方法是转到系统,高级系统设置,在系统属性窗口,高级选项卡,环境变量…

然后在我的C:\Users\<你的用户名>中,我创建了文件.bashrc,例如,触摸.bashrc并添加所需的别名。

访问:C:\Program Files\Git\etc\profile.你在里面找到你需要的一切。 或者在您的主目录中创建.bashrc和.bash_profile。

. bash_profile

# generated by Git for windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

. bashrc

echo "Hello World!"

在新版本的Git for Windows中,Bash以——login启动,这导致Bash不能直接读取.bashrc。相反,它读取.bash_profile。

如果~ /。Bash_profile文件不存在,按照如下内容创建Bash_profile文件:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

这将导致Bash读取.bashrc文件。根据我对这个问题的理解,Git for Windows应该自动完成这个任务。但是,我刚刚安装了2.5.1版本,它没有。

有时文件实际上位于~/。以下是我在Visual Studio Code/Windows 10上启动Zsh作为默认终端的步骤。

cd ~ / vim . bashrc 粘贴下面的…

如果test -t 1;然后 exec zsh fi

保存/关闭Vim。 重启终端