ubuntu bash环境设置

第一步  系统级设置

读取/etc/profile文件,再根据/etc/profile文件的内容去读取其他附件的设置文件,例如/etc/profile.d/*.sh等


第二步  个人设置

根据不同的用户,到家目录去读取~/.bash_profile或~/.bash_login或~/.profile设置文件
其中:
这三个文件通常只要一个即可,不过存在顺序上的差异:bash启动时,会先去读取~/.bash_profile,找不到时就去读取~/.bash_login,再找不到时则读取~/.profile

根据不同的用户,到家目录去读取~/.bashrc文件


所以登陆bash后,最终读取的设置文件是~/.bashrc,通常可以将个人的一些常用的alias或者PATH等环境变量或者自定义变量都写到这个文件。


ps: 可能需要注意区分login shell 与 non-login shell

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to the terminal , or one started with the -i option.

Login Shell: 登录的时候,你输入帐号和密码进入了,出现了shell提示符,这个过程就是登录外壳;
Interactive Shell:接下来你得到了一个像root@host />这样的提示符,你输入什么,它就解释出什么,这就是交互式外壳;
Non Interactive Shell:你可能需要学习写一个bash script,用外部shell执行:bash  script.sh,它(bash)从第一条命令执行到最后一条然后退出,不与你进行任何交互,它就是非交互式外壳

详见:

man 1 bash 之 INVOCATION

" a non-login shell, which does not read /etc/profile or ~/.bash_profile files, but rather reads the ~/.bashrc instead."

 

例子:

新建用户 lfs
useradd -s /bin/bash - lfs -m -k /dev/null lfs

在用户lfs的根目录中新建.bashrc文件,输入:
LFS=/mnt/lfs

以root身份使用命令su lfs(非登录shell)
echo $LFS
/mnt/lfs

以root身份使用命令su - lfs (登录shell)
echo $LFS