Linux怎么设置配置文件/profile .bash_profile .bash_login .profile .bashrc

761 阅读1分钟

区别

"~"符号就是各当前用户的$HOME eg:

cd ~
pwd

代表 /home/username/

  • 整个系统所有用户:/etc/profile

  • 各个用户:~/.bashrc ,~/.bash_profile和~/.profile

    • 每次都读取:~/.bashrc

    • 只在登陆时读取一次: ~/.bash_profile 和 ~/.profile

      • 只bash读取:~/.bash_profile
      • 其他shell 也读:~/.profile

登陆模式读取区别

1.login shell

包括interactive shell或者non-interactive shell

  • /etc/profile

全局的配置,不管哪个用户登录,都会读取

  • ~/.bash_profile 或~/.bash_login 或~/.profile

1.按照顺序 找到其中任意一个即执行读取,不会再找下一个了。 2.这三个文件的内容都是去读取~/.bashrc这个文件

 # .bash_profile
  2 
  3 # Get the aliases and functions
  4 if [ -f ~/.bashrc ]; then
  5         . ~/.bashrc
  6 fi
  7 
  8 # User specific environment and startup programs

2. non-login shell

2.1 interactive non-login shell

只会读取配置~/.bashrc

2.2 non-interactive non-login shell

该模式即最普通的shell脚本,如: bash tesh.sh

不读取任何配置文件,而是会读取环境变量 BASH_ENV所指向的脚本文件

//创建临时环境变量BASH_ENV
[root@xxx~]# export BASH_ENV='~/hello.sh'

//编写脚本hello.sh  内容如下
1 #!/bin/bash
2 echo "this is hello.sh"

//编写脚本test.sh  内容如下
1 #!/bin/bash
2 echo "this is test.sh"

[root@xxx ~]# bash test.sh 
this is hello.sh
this is test.sh

修改

  1. /etc/profile尽量不修改
  2. 修改用户目录下的~/.bashrc来新增或者修改环境变量

from: inux环境变量之profile .bash_profile .bash_login .profile .bashrc 加载详解 登陆shell,非登陆shell 以及交互shell和非交互shell