频道
bg

Bash Startup Files

coding三月 03, 20211mins
OP

ShellH1

  • Interactive Shells /etc/bash.bashrc~/.bashrc
    • Login Shells /etc/profile~/.bashprofile, ~/.bashlogin, ~/.profile
    • Non Login Shells
  • Non-interactive Shells $BASH_ENV When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.

    java

    if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

Environment VariableH1

理论上,任何 shell 脚本都可以初始化环境变量,但为了维护方便,环境变量的定义集中在几个特定的文件中。对全局变量来说是:/etc/profile, /etc/bash.bashrc 和 /etc/environment. 每个文件都有不同的限制,请根据需要选择要使用的文件。

  • /etc/profile 初始化登陆 shell 的环境变量。它可以执行脚本并支持 Bash 兼容 Shell。
  • /etc/bash.bashrc 初始化交互 shell,它也可以执行脚本但是只支持 Bash。
  • /etc/environment 被 PAM-env 模块使用,和登陆与否,交互与否,Bash与否无关,所以无法使用脚本或通配符展开。仅接受 variable=value 格式。 以将 ~/bin 加入某些特定用户的 PATH 为例,可以将其放入 /etc/profile 或 /etc/bash.bashrc:

    java

    # If user ID is greater than or equal to 1000 & if ~/bin exists and is a directory & if ~/bin is not already in your $PATH
    # then export ~/bin to your $PATH.
    if [[ $UID -ge 1000 && -d $HOME/bin && -z $(echo $PATH | grep -o $HOME/bin) ]]
    then
    export PATH=$HOME/bin:${PATH}
    fi

参考H1

[2]: https://wiki.archlinux.org/index.php/Environmentvariables(简体中文)

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.