理解bashrc和profile:Linux中bashrc和profile的区别

洼地云 ai-quyi.png

在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile 来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文件都有什么作用和该如何使用呢?

常见操作系统中的bashrc和profile

我们先来看一下常见的Linux系统中的bashrc和profile:

在CentOs中:

/etc/profile
/etc/bashrc

~/.bashrc
~/.bash_profile

在Ubuntu,Debian中:

/etc/profile
/etc/bash.bashrc

~/.bashrc
~/.profile

Shell的种类

在理解这些文件之前还需要了解Shell的 login(登入) 和 interactive(交互式) 模式.

login shell 和 no-login shell

Linux系统中shell分为很多种,比如bash,sh,zsh之类的,如果想要查看一个用户使用的是什么shell,可以通过finger [username]命令来查看。我们这里只说shell是bash的情况,因为如果是sh或者其他shell,显然不会运行bashrc的。

“login shell” 代表用户登入, 比如使用 “su -” 命令, 或者用 ssh 连接到某一个服务器上, 都会使用该用户默认 shell 启动 login shell 模式,该模式下的shell会去自动执行 /etc/profile 和 ~/.profile 文件,但不会执行任何的bashrc文件,所以一般在 /etc/profile 或者 ~/.profile 里我们会手动去 source bashrc 文件。

而 no-login shell 的情况是我们在终端下直接输入 bash 或者 bash -c “CMD” 来启动的 shell,该模式下是不会自动去运行任何的 profile 文件。

interactive shell 和 non-interactive shell

interactive shell 是交互式shell,顾名思义就是用来和用户交互的, 提供了命令提示符可以输入命令,该模式下会存在一个叫 PS1 的环境变量, 如果还不是 login shell 的则会去 source /etc/bash.bashrc 和 ~/.bashrc 文件。
non-interactive shell 则一般是通过 bash -c “CMD” 来执行的bash,该模式下不会执行任何的 rc 文件, 不过还存在一种特殊情况这个我之后详细讲述。

参考文章:

  1. https://wido.me/sunteya/understand-bashrc-and-profile/ ;
  2. http://opsmysql.blog.51cto.com/2238445/665990 ;
赞(0)
未经允许禁止转载:优米格 » 理解bashrc和profile:Linux中bashrc和profile的区别

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册