为什么人们从 bashrc 获取 bash_profile 而不是相反?

iva*_*van 7 linux terminal bash shell bashrc

听起来大多数终端模拟器默认情况下不会以登录身份运行本地会话,因此它们将加载 bashrc 而不是 bash_profile。那么为什么大多数人将所有内容都放在 bash_profile 中并让 bashrc 提供它,而不是相反呢?“大多数人”是指迄今为止我见过的大多数人。也许它没有我想象的那么普遍。

与其将我们的配置放在那里并拥有 bashrc 源 bash_profile,将所有内容都放在 bashrc 中并拥有 bash_profile 源不是更有意义并且与 linux 社区更一致吗?

我听说过关于 iTerm2 的好消息,听起来是这样,几乎所有其他终端仿真器(默认的 OSX 终端除外)都会在我本地运行时加载 bashrc。并不重要,只要一个来源另一个,但我很困惑为什么更喜欢 bash_profile 是标准?

次要注意事项:我对 iTerm2 有误解。它默认运行登录会话,就像 Terminal.app 一样,尽管两个模拟器似乎都有一个选项可以让你改变它。

Bil*_*key 11

由于本地约定,人们从 bashrc 获取 bash_profile 而不是相反。

我读过的关于如何配置他们的启动文件的所有意见bash主要基于本地约定。本地惯例通常不会提及大局,因为它没有过多谈论非登录、非交互情况。有趣的是,我已经看过了,但我很少看到有人cron在他们的所有讨论中提到为什么将变量放在一个启动文件中而不是另一个中。事实上,我没有听到任何评论说:“ /bin/sh 是有原因的。Bash 模拟原始的 Bourne shell,/bin/sh,当这样调用时。 ”一方面,我稍微离题了,这种情况对于不仅以交互方式使用外壳而且提供非交互方式的人来说很重要,background )cron 需要最少 shell 处理的脚本,即后台处理不需要彩色提示、命令历史和替换、正确定义的 $TERM 变量等细节。

此外,关于cron,我通常看到的是人们在工作时创建最小搜索路径或调用完全合格的程序,并且不知道如何处理未连接到终端的输出(即非交互式、非登录bashsh案例)用他们的cron脚本。这通常是因为对 shell 启动顺序没有很好的理解,导致用户以与本地/etc启动文件中已经建立的约定不一致或不连贯的方式实现自己的启动文件。

详细说明,按本地约定完成的设置在该特定安装和 shell/etc文件中进行了布局。如果检查任何 UNIX 安装/etc文件,这些文件作为典型bash启动序列的一部分被调用,则应该以一种与这些/etc启动文件中建立的约定互补的方式创建自己的启动。

Linux 文档项目指出:

/etc/skel/ 每个新用户的默认文件都存储在此目录中。每次添加新用户时,这些骨架文件都会复制到其主目录中。一个普通的系统应该有:.alias、.bash_profile、.bashrc 和 .cshrc 文件。其他文件由系统管理员决定。

虽然bash手册没有/etc/skel明确提到这些在目录中常见的文件,但据我所知,SunOS、Solaris、RedHat、Ubuntu、HP-UX、umips 和 Ultrix 有/etc/skel文件可以在之后对用户的 shell 启动文件进行模式化。OSX 显然没有 - 我现在正在使用 OSX 10.9.1。不幸的是,OSX 并没有给你太多关于如何按照约定进行设置的内容,但由于 OSX 是 BSD 的衍生产品,我只是使用了另一个 BSD 衍生产品,并bash在此之后设计了我自己的启动顺序,调整它符合 OSX 10.9.1/etc启动文件中使用的本地约定。

在并行评论中提到的一个重要点是,对于 OSX,约定是将每个新终端作为交互式登录 shell 启动。这确实是 OSX 中的默认设置。只要一个安装的用户是一致的,这个约定就没有问题。 通过对终端的首选项进行以下更改,特别是更改设置Shells open with:以发出/usr/bin/login -f -l whmcclos bash -i命令,可以更改 OSX 上终端的默认行为以符合其他 UNIX 发行版的 shell 启动约定

在此处输入图片说明

有了所有这些作为背景或介绍,我将继续我的最佳建议,它的价值。

我最好的建议:

检查 UNIX 发行版的管理员已放置的文件。从以下位置开始(如果存在)。不要忘记使用该ls -a命令,因为有些文件以点开头。查看这些文件在启动过程中是如何使用的,并查看您自己的启动文件如何与它们交互:

/etc/bashrc
/etc/profile
/etc/skel/.bash_logout
/etc/skel/.bashrc
/etc/bash.bashrc
/etc/bash_completion
Run Code Online (Sandbox Code Playgroud)

查看bash手册中的调用和启动顺序。这一切都布置得很好。

所有这些都作为警告 - 这是我在 OSX 10.9.1 安装上的工作方式 - 其他 UNIX 发行版会有所不同,但下面介绍的内容应该适用于大多数(如果不是全部)UNIX 发行版,但使用那些其他 UNIX 发行版”约定作为指南,根据您自己的目的定制以下内容:

。轮廓

# ~/.profile: executed by the command interpreter for login shells.

# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.  Note, however, that we will have a ~/.bash_profile and it
# will simply source this file as a matter of course.

# See /usr/share/doc/bash/examples/startup-files for examples.
# The files are located in the bash-doc package.

# From here on out, I basically set up my PATH, LD_LIBRARY_PATH, and anything else I'd like
# global to running programs and how those programs find their libraries.  This is shared by
# `cron`, so we really don't want interactive stuff, here.  Also, I setup my environments
# for brew, macports, and fink here, essentially with setting PATH, and invocation of those
# package initialization file as in:

# Brew and locally compiled stuff:
export PATH=/usr/local/bin:$PATH
export PATH=/usr/local/sbin:$PATH

# The following line puts gnu utilities without the prefix "g" in the path
# i.e. tar/gtar:
export PATH=$PATH:/usr/local/Cellar/coreutils/8.21/libexec/gnubin

# MacPorts shoves stuff in /opt, so to get at that stuff...
export PATH=/opt/local/bin:$PATH
export PATH=/opt/local/sbin:$PATH

# Set up for using Fink, which lives in /sw:
[ -e /sw/bin/init.sh ] && . /sw/bin/init.sh

# My stuff:
export PATH=~/perl:$PATH
export PATH=~/bin:$PATH
export PATH=.:$PATH
Run Code Online (Sandbox Code Playgroud)

.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

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# From here on out, I put in things that are meaningful to interactive shells, like aliases,
# `shopt` invocations, HISTORY control, terminal characteristics, PROMPT, etc.
Run Code Online (Sandbox Code Playgroud)

.bash_profile

# ~/.bash_profile: executed by the command interpreter for login shells.

# Because of this file's existence, neither ~/.bash_login nor ~/.profile
# will be sourced.

# See /usr/share/doc/bash/examples/startup-files for examples.
# The files are located in the bash-doc package.

# Because ~/.profile isn't invoked if this files exists,
# we must source ~/.profile to get its settings:
if [ -r ~/.profile ]; then . ~/.profile; fi

# The following sources ~/.bashrc in the interactive login case,
# because .bashrc isn't sourced for interactive login shells:
case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac

# I'm still trying to wrap my head about what to put here.  A suggestion
# would be to put all the `bash` prompt coloring sequence functions as
# described on http://brettterpstra.com/2009/11/17/my-new-favorite-bash-prompt/
Run Code Online (Sandbox Code Playgroud)

所以这是我的两分钱。请记住,我的示例试图通过启动文件显示控制路径,并避免任何特定站点的约定可能强加的内容。