打开新终端时未执行 .bashrc

Sel*_*lah 124 bashrc

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

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

我该如何做到这一点?

Mar*_*ied 184

它不一定是运行;在标准 .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
Run Code Online (Sandbox Code Playgroud)

I believe there is an option to run bash terminal as a login shell or not. With Ubuntu, gnome-terminal does not normally run as a login shell, so .bashrc should be run directly.

For login shells (like the virtual terminals), normally the file ~/.profile is run, unless you have either ~/.bash_profile or ~/.bash_login, but they are not there by default. By default, Ubuntu uses only .profile.

The standard ~/.profile has this in it:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi
Run Code Online (Sandbox Code Playgroud)

This runs .bashrc if it is available - assuming $BASH_VERSION is present in your environment. You can check for this by entering the command echo $BASH_VERSION, and it should display some information on version number - it should not be blank.

  • 它_通常_在您启动新的登录 shell 时运行(不是真正的桌面会话,因为您可以从桌面会话运行新的登录 shell)。正如我所说,它默认运行,但如果您创建了`~/.bash_profile` 或`~/.bash_login`,则不会运行。您可以通过选中或取消选中`Edit -> Profile Preferences -> Title and Command -> "Run command as a login shell` 中的复选框来进行测试,然后退出然后重新运行终端。您可以从 .profile 回显某些内容进行测试。 (5认同)
  • 理解这些说明后,我进入我的终端应用程序,我去编辑 -> 配置文件首选项 -> 标题和命令 -> 作为登录 shell 运行命令。我取消了这个。谢谢您的帮助。 (4认同)
  • 因此,如果您在系统上创建了 `~/.bash_profile` 或 `~/.bash_login` 并希望保留它们,但仍然想要 `~/.profile` 并从那里执行 `~/.bashrc`,您可以使用 source它来自您自己的`~/.bash_profile`或`~/.bash_login`,其中包含包含`source "$HOME/.profile"`的行。在登录 shell 中也给您返回 ls 的彩色输出! (2认同)

小智 49

就我而言,只是.bashrc缺少加载程序行.bash_profile

# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
fi
Run Code Online (Sandbox Code Playgroud)

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

  • `.bash_profile`?Ubuntu 默认使用`.profile`,而不是`.bash_profile`。 (3认同)

SD.*_*SD. 11

.bash_profile保存 bash shell 的配置。当您打开终端时,它首先从~/.bash_profile. 所以你可以.bash_profile根据bashrc添加以下内容来设置shell。

. ~/.bashrc
Run Code Online (Sandbox Code Playgroud)


Nic*_*vit 10

根据评论中.profile

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
Run Code Online (Sandbox Code Playgroud)

就这样吧;如果~/.bash_profile~/.bash_login存在,这些将被运行而不是~/.profile.

如果您想运行,~/.bashrc只需source ~/.bashrcbash_profile.


小智 5

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

我在 12.04 LTS 上遇到了类似的问题,结果发现新用户帐户的默认 shell 设置为/bin/sh,这就是问题的原因。