打开新终端时没有执行.bashrc

当我在Ubuntu 12.04中打开一个新的终端窗口时,.bashrc中的代码不会执行。 我在创建.bash_aliases文件时注意到了这一点。 当我打开一个新终端时,别名没有出现。 但是,当我键入source .bashrc ,别名确实显示出来。

每次打开一个新的终端窗口时都应该运行.bashrc吗?

我该如何做到这一点?

它不一定是运行; 在标准.bashrc的顶部是这个评论:

 # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples 

我相信有一个选项可以将bash终端作为登录shell运行。 使用Ubuntu,gnome-terminal通常不作为登录shell运行,因此.bashrc应该直接运行。

对于登录shell(如虚拟终端),通常会运行~/.profile文件,除非您有~/.bash_profile~/.bash_login ,但默认情况下它们不存在。 默认情况下,Ubuntu仅使用.profile。

标准~/.profile有这个:

 if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi 

如果可用,则运行.bashrc – 假设您的环境中存在$ BASH_VERSION。 您可以通过输入命令echo $BASH_VERSION来检查这echo $BASH_VERSION ,它应该显示有关版本号的一些信息 – 它不应该是空白的。

在我的例子中, .bash_profile只缺少.bashrc加载行

 # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi 

我手动添加它,它与我的新登录一起工作

如果未设置$BASH_VERSION ,请尝试使用chsh命令将shell设置为/bin/bash

我有一个与12.04 LTS类似的问题,结果发现新用户帐户的默认shell设置为/bin/sh ,这是导致问题的原因。

.bash_profile保存bash shell的配置。 当您打开终端时,它首先从~/.bash_profile读取并执行命令。 因此,您可以在.bash_profile添加以下内容,以根据bashrc设置shell。

 . ~/.bashrc 

只需转到Edit -> Profile Preferences -> Title and Command -> "Run a custom command instead of my shell"然后在自定义命令框中写入bash并关闭它,而不是全部完成。 下次你打开终端时,它也会自动运行bash。