[关闭]
@blackwang 2016-02-20T23:58:26.000000Z 字数 1270 阅读 1246

【.bash_profile 与 .bashrc 的区别】

Linux .bashrc .bash_profile


.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

【login shell 与 non-login shell 的区别】

1、当你直接在机器login界面登陆、使用ssh登陆或者su切换用户登陆时,.bash_profile 会被调用来初始化shell环境

Note:.bash_profile文件默认调用.bashrc文件

.bash_profile中有如下内容

  1. if [ -f ~/.bashrc ]; then
  2. . ~/.bashrc
  3. fi

2、当你不登陆系统而使用ssh直接在远端执行命令,.bashrc 会被调用
3、当你已经登陆系统后,每打开一个新的Terminal时,.bashrc 都会被再次调用。

测试准备工作

hclient2主机hadoop用户家目录下执行

  1. [hadoop@hclient2 ~]$ echo "invoke hclient2:~/.bashrc">>.bashrc
  2. [hadoop@hclient2 ~]$ echo "invoke hclient2:~/.bash_profile">>.bash_profile

Login Shell

1、窗口登陆

  1. Red Hat Enterprise Linux Server release 6.3 (Santiago)
  2. Kernel 2.6.32-279.el6.x86_64 on an x86_64
  3. hclient2 login: hadoop
  4. Password:
  5. Last login: Mon Feb 25 23:03:45 on tty1
  6. invoke hclient2:~/.bashrc
  7. invoke hclient2:~/.bash_profile
  8. [hadoop@hclient2 ~]$

2、SSH 登陆

  1. [hadoop@hserver ~]$ ssh hclient2
  2. Last login: Mon Feb 25 22:42:19 2013 from hserver
  3. invoke hclient2:~/.bashrc
  4. invoke hclient2:~/.bash_profile
  5. [hadoop@hclient2 ~]$

3、su 登陆

  1. [root@hclient2 ~]# su - hadoop
  2. invoke hclient2:~/.bashrc
  3. invoke hclient2:~/.bash_profile

Non-login Shell:

Note: ssh ...[user@] hostname [command]
If command is specified, it is executed on the remote host instead of a login shell.
[hadoop@hserver ~]$ ssh hclient2 hostname
invoke hclient2:~/.bashrc
hclient2

【故】若要配置环境变量之类,最保险是写在 .bashrc 文件中。因为不管是登陆还是不登陆,该文件总会被调用!

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注