解释〜/ .profile文件的内容

# ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. # see /usr/share/doc/bash/examples/startup-files for examples. # the files are located in the bash-doc package. # the default umask is set in /etc/profile; for setting the umask # for ssh logins, install and configure the libpam-umask package. #umask 022 # if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi 

任何人都可以解释~/.profile文件的内容吗? 所以当你进入~/.profile文件时,所有的写作意味着什么?

简化版:

 if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi 

这部分检查了什么~/.profile本身是由Bash实例提供的,如果是这种情况来源依次~/.bashrc ; 这是一种包含存储在~/.bashrc的用户设置的方法,例如也包含在登录shell中,通常不会提供~/.bashrc ;

 if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi 

这部分检查是否存在~/bin ,如果是这种情况,则将~/bin$PATH的当前值; 这样做是为了使~/bin存在的潜在可执行文件/脚本优先于$PATH包含的其他路径中存在的可执行文件/脚本(例如,通过在~/bin放置一个名为cat的可执行文件,当运行cat ,可执行文件将在其中运行)通常的地方/bin/cat )。


详细版本:

 if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi 

这部分检查$BASH_VERSION的扩展是否具有非零长度( if [ -n "$BASH_VERSION" ] ),如果是这种情况,如果$HOME/.bashrc的扩展存在并且是常规文件( if [ -f "$HOME/.bashrc" ] ), $HOME/.bashrc的扩展来源。

由于Bash在调用时设置了$BASH_VERSION ,因此检查$BASH_VERSION是否具有非零长度是确定文件本身是否由Bash实例提供的有效方法。

这就是为什么当在Ubuntu上调用Bash作为登录shell时,包含存储在~/.bashrc中的用户设置(对于其他发行版不一定如此); 当作为登录shell调用时,Bash本身只会提供~/.profile ,这是一种绕过它的方法;

 if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi 

这部分检查$HOME/bin的扩展是否存在并且是一个目录( if [ -d "$HOME/bin" ] ),如果是这种情况,则将$HOME/bin的扩展预先设置为$PATH的当前值( PATH="$HOME/bin:$PATH" ; $HOME通常设置为用户的主目录)。

这样做是为了使$HOME/bin扩展中存在的潜在可执行文件/脚本优先于$PATH包含的其他路径中存在的可执行文件/脚本。

Interesting Posts