.bashrc、.bash_profile 和 .environment 之间有什么区别? [英] What's the difference between .bashrc, .bash_profile, and .environment?

查看:27
本文介绍了.bashrc、.bash_profile 和 .environment 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些年来我使用了许多不同的基于 *nix 的系统,似乎我使用的每一种 Bash 都有不同的算法来决定运行哪些启动脚本.出于设置环境变量和别名以及打印启动消息(例如 MOTD)等任务的目的,哪个启动脚本适合执行这些操作?

.bashrc.bash_profile.environment 中放入东西有什么区别?我还看到了其他文件,例如 .login.bash_login.profile;这些是否相关?在物理登录、通过 ssh 远程登录和打开新的终端窗口时运行的有什么区别?跨平台(包括 Mac OS X(及其 Terminal.app)和 Cygwin Bash)是否存在显着差异?

解决方案

与 shell 配置文件的主要区别在于,有些配置文件只能由登录"shell 读取(例如,当您从另一台主机登录时,或在文本中登录时本地 unix 机器的控制台).这些是称为 .login.profile.zlogin 的那些(取决于您使用的外壳).

然后你有由交互式"外壳读取的配置文件(例如,连接到终端(或伪终端,例如,在窗口系统下运行的终端模拟器).这些是名称如.bashrc.tcshrc.zshrc

bash 使情况复杂化,因为 .bashrc 仅由 交互式非登录的 shell 读取,所以你会发现大多数人最终会告诉他们的 .bash_profile 也读取 .bashrc 类似的东西

[[ -r ~/.bashrc ]] &&.~/.bashrc

其他 shell 的行为有所不同 - 例如使用 zsh,对于交互式 shell,无论是否登录,总是读取 .zshrc.

bash 的手册页解释了读取每个文件的情况.是的,机器之间的行为通常是一致的.

.profile 只是 /bin/sh 最初使用的登录脚本文件名.bash,通常与 /bin/sh 向后兼容,如果存在,将读取 .profile.

I've used a number of different *nix-based systems of the years, and it seems like every flavor of Bash I use has a different algorithm for deciding which startup scripts to run. For the purposes of tasks like setting up environment variables and aliases and printing startup messages (e.g. MOTDs), which startup script is the appropriate place to do these?

What's the difference between putting things in .bashrc, .bash_profile, and .environment? I've also seen other files such as .login, .bash_login, and .profile; are these ever relevant? What are the differences in which ones get run when logging in physically, logging in remotely via ssh, and opening a new terminal window? Are there any significant differences across platforms (including Mac OS X (and its Terminal.app) and Cygwin Bash)?

解决方案

The main difference with shell config files is that some are only read by "login" shells (eg. when you login from another host, or login at the text console of a local unix machine). these are the ones called, say, .login or .profile or .zlogin (depending on which shell you're using).

Then you have config files that are read by "interactive" shells (as in, ones connected to a terminal (or pseudo-terminal in the case of, say, a terminal emulator running under a windowing system). these are the ones with names like .bashrc, .tcshrc, .zshrc, etc.

bash complicates this in that .bashrc is only read by a shell that's both interactive and non-login, so you'll find most people end up telling their .bash_profile to also read .bashrc with something like

[[ -r ~/.bashrc ]] && . ~/.bashrc

Other shells behave differently - eg with zsh, .zshrc is always read for an interactive shell, whether it's a login one or not.

The manual page for bash explains the circumstances under which each file is read. Yes, behaviour is generally consistent between machines.

.profile is simply the login script filename originally used by /bin/sh. bash, being generally backwards-compatible with /bin/sh, will read .profile if one exists.

这篇关于.bashrc、.bash_profile 和 .environment 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆