当前位置: 首页>>技术教程>>正文


为什么Ubuntu默认的〜/.profile源〜/.bashrc?

, , ,

问题描述

这些是我的13.10附带的库存~/.profile的内容(删除了注释行):

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

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

这是从Debian继承的,但是Canonical为什么决定保留它?据我所知,这不是标准的* nix方式,而且我已经看到各种系统都没有发生这种情况,因此我认为它们一定有充分的理由。在运行用户不希望获得~/.bashrc的登录Shell(例如,将其装入计算机)时,这可能导致意外行为。

我能想到的唯一好处是,不会使用户混淆许多启动文件,并允许他们单独编辑.bashrc并可以读取该文件,而与shell类型无关。但是,这是一个可疑的好处,因为对登录名和交互式Shell使用不同的设置通常很有用,这会阻止您这样做。此外,登录 shell 程序通常不在图形环境中运行,这可能会导致错误,警告和问题(哦,天哪!),具体取决于您在这些文件中设置的内容。

那么为什么Ubuntu会这样做,我想念的是什么?

最佳思路

这是Debian做出的上游决定。在非常好的wiki post中对此进行了解释,以下为摘录。执行摘要是“确保GUI和非GUI登录以相同的方式工作”:

Let’s take xdm as an example. pierre comes back from vacation one day and discovers that his system administrator has installed xdm on the Debian system. He logs in just fine, and xdm reads his .xsession file and runs fluxbox. Everything seems to be OK until he gets an error message in the wrong locale! Since he overrides the LANG variable in his .bash_profile, and since xdm never reads .bash_profile, his LANG variable is now set to en_US instead of fr_CA.

Now, the naive solution to this problem is that instead of launching “xterm”, he could configure his window manager to launch “xterm -ls”. This flag tells xterm that instead of launching a normal shell, it should launch a login shell. Under this setup, xterm spawns /bin/bash but it puts “-/bin/bash” (or maybe “-bash”) in the argument vector, so bash acts like a login shell. This means that every time he opens up a new xterm, it will read /etc/profile and .bash_profile (built-in bash behavior), and then .bashrc (because .bash_profile says to do that). This may seem to work fine at first — his dot files aren’t heavy, so he doesn’t even notice the delay — but there’s a more subtle problem. He also launches a web browser directly from his fluxbox menu, and the web browser inherits the LANG variable from fluxbox, which is now set to the wrong locale. So while his xterms may be fine, and anything launched from his xterms may be fine, his web browser is still giving him pages in the wrong locale.

So, what’s the best solution to this problem? There really isn’t a universal one. A better approach is to modify the .xsession file to look something like this:

[ -r /etc/profile ] && source /etc/profile [ -r ~/.bash_profile ] && source ~/.bash_profile xmodmap -e 'keysym Super_R = Multi_key' xterm & exec fluxbox 

This causes the shell that’s interpreting the .xsession script to read in /etc/profile and .bash_profile if they exist and are readable, before running xmodmap or xterm or “execing” the window manager. However, there’s one potential drawback to this approach: under xdm, the shell that reads .xsession runs without a controlling terminal. If either /etc/profile or .bash_profile uses any commands that assume the presence of a terminal (such as “fortune” or “stty”), those commands may fail. This is the primary reason why xdm doesn’t read those files by default. If you’re going to use this approach, you must make sure that all of the commands in your “dot files” are safe to run when there’s no terminal.

次佳思路

这是Ubuntu的标准行为,~/.bashrc是user-level per-interactive-shell启动文件。基本上,当您打开一个终端时,您将启动一个读取~/.bashrcnon-login, interactive shell,并将~/.bashrc的内容作为源并将其导出到当前的Shell环境中。它有助于一个人在当前shell中获取其所有用户定义的shell变量和函数。你也可以找到这样的行

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

在当前的shell环境中获取用户定义的别名。

这对于提供良好的用户体验也很重要。例如,一个代理凭证可以存储在.bashrc中,除非它从终端应用程序(viz,pingwgetcurllynx等)中获取,否则将无法正常工作。或者,您每次打开终端时都必须提供代理凭据。

Ubuntu的默认.bashrc除了包含许多用户友好的别名(用于lsgrep以打印彩色输出)外,还为不同的Shell变量提供了许多新的定义,从而增加了用户体验。

但是,如果使用ssh登录或在虚拟控制台中登录,则基本上可以获得交互式登录shell。那里的 shell 启动文件是~/.profile。因此,除非您提供~/.bashrc,否则您会错过.bashrc中所有有用的设置。这就是为什么Ubuntu默认的~/.profile~/.bashrc

应避免的情况

  • ~/.profile获取~/.bashrc时,切勿同时在~/.bashrc内获取~/.profile表单。它将形成无限循环,结果除非您按下Ctrl + C,否则终端提示将被挂起。在这种情况下,如果您在~/.bashrc中放置一行

set -x

然后,您可以看到打开终端时文件描述符正在停止。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/11438.html,未经允许,请勿转载。