Difference between unix-like shell profile scripts

《Difference between unix-like shell profile scripts》

首先,先来区分几个概念:

  1. /etc/profile

    不建议随意修改此文件

    全局(公有)配置,系统所有用户登录时都会读取该文件。
    修改此文件,系统所有用户都会受影响。
    所以如果你有对 /etc/profile 有修改的话必须得重启你的修改才会生效,此修改对每个用户都生效。

    # /etc/profile
    
    # System wide environment and startup programs, for login setup
    # Functions and aliases go in /etc/bashrc
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
  2. /etc/bashrc

    系统级环境变量

    [Deprecated]

    全局(公有)配置,系统所有用户在执行 bash shell 时都会读取此文件。
    修改此文件,系统所有用户的 bash shell 配置都会受影响。
    如果你想对所有的使用 bash shell 的用户修改某个配置并在以后打开的bash都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个 bash shell 即可生效。

    # /etc/bashrc
    
    # System wide functions and aliases
    # Environment stuff goes in /etc/profile
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
  3. ~/.profile or ~/.bash_profile

    用户级环境变量

    Unix/Linux有两个profile文件:

    • /etc/profile: 是全局 profile 文件,设置后会影响到所有用户
    • ~/.profile~/.bash_profile 是针对特定用户的,可以针对用户,来配置自己的环境变量。
    • ~/.profile 是Unix上才有的;
    • ~/.bash_profile 是Linux下有的(Linux下,没有 ~/.profile 文件)
    • ~/.profile/.bash_profile,都是隐藏文件,需要使用 ls -a 才能看到。

    ~/.profile on Debian/Ubuntu
    ~/.bash_profile on CentOS/Fedora/RedHat.

    用户可以在 profile 文件中加入环境变量,比如ORACLE_HOME,HOME…这样重新登录之后,这些环境变量都会得以设置,不用每次都手工设置。
    局部(私有)配置,系统当前用户在执行 bash shell 时会读取此文件。
    修改此文件,只有系统当前用户的 bash shell 配置会受到影响。

    每个用户都可使用该文件输入专用于自己使用的 shell 信息, 当用户登录时, 该文件仅仅执行一次!
    /etc/profile对所有用户生效,~/.bash_profile 只对当前用户生效。
    此文件类似于 /etc/profile,也是需要重启才会生效

    但是有时在 .bash_profile 文件中的环境变量并没有起到作用, 这时可以查看使用的Mac OS X是什么样的Shell。

    ➜ ~ echo $SHELL
    /bin/zsh

    当mac上安装了zsh后,修改环境变量就需要在 ~/.zshrc 中修改,比如:

    export http_prox=http://10.199.75.12:8080
    export https_proxy=http://10.199.75.12:8080

    如果想要修改立即生效,需要执行

    source ~/.zshrc
  4. ~/.bashrc or ~/.zshrc

    用户级环境变量

    每个用户都有一个 .bashrc 文件,在用户目录下。

    这个文件主要保存个人的一些个性化设置,如命令别名、路径等。定义了路径,语言,命令别名(使用rm删除命令时总是加上-i参数需要用户确认,使用ls命令列出文件列表时加上颜色显示)。
    每次修改 .bashrc 后,使用 source ~/.bashrc(或者 . ~/.bashrc)就可以立刻加载修改后的设置,使之生效。
    一般会在 .bash_profile 文件中显式调用 .bashrc
    登陆linux启动bash时首先会去读取 /.bash_profile 文件,这样 /.bashrc 也就得到执行了,你的个性化设置也就生效了。

至于
~/.profile
~/.bashrc, 它们都具有个性化定制功能.

~/.profile 可以设定本用户专有的路径,环境变量,等,它只能登入的时候执行一次.

~/.bashrc 也是某用户专有设定文档,可以设定路径,命令别名,每次shell script的执行都会使用它一次

Bash登陆(login)的时候,Profile执行的顺序:

  1. 先执行 /etc/profile
  2. 接着bash会检查使用者的HOME目录中,是否有 .bash_profile 或者 .bash_login 或者 .profile,若有,则会执行其中一个,执行顺序为:.bash_profile 最优先 > .bash_login 其次 > .profile 最后
  3. 然后在根据用户帐号读取 ~/.bashrc
    原文作者:Yahkun
    原文地址: https://segmentfault.com/a/1190000016326642
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞