详细介绍可以察看info bash中的INVOCATION部分。此处仅作简要总结。
The difference between a login shell and a non-login shell (info bash)
login shell
*) interactive login shell
*) non-interactive shell with --login option
*) behavior
*) it reads and executes /etc/profile if exists
*) it looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in that
order and reads and executes the first one found
non-login shell
*) interactive non-login shell
it reads and executes /etc/bash.bashrc and ~/.bashrc
*) non-interactive non-login shell
it first executes the following command before running the script
if [ -n "$BASH_ENV" ]; then . $BASH_ENV; fi
举例说明下有点“奇怪”的non-interactive non-login shell的行为(这是我们通常执行脚本时的默认模式)。
chenqi@chenqi-Vostro-2420:~/tests$ cat test.sh
#!/bin/sh
echo hello world
chenqi@chenqi-Vostro-2420:~/tests$ echo $BASH_ENV
chenqi@chenqi-Vostro-2420:~/tests$ ./test.sh
hello world
chenqi@chenqi-Vostro-2420:~/tests$ export BASH_ENV=~/tests/test.sh
chenqi@chenqi-Vostro-2420:~/tests$ echo $BASH_ENV
/home/chenqi/tests/test.sh
chenqi@chenqi-Vostro-2420:~/tests$ ./test.sh
hello world
chenqi@chenqi-Vostro-2420:~/tests$ bash test.sh
hello world
hello world
将test.sh的默认执行shell改成/bin/bash,有如下结果:
chenqi@chenqi-Vostro-2420:~/tests$ cat test.sh
#!/bin/bash
echo hello world
chenqi@chenqi-Vostro-2420:~/tests$ ./test.sh
hello world
hello world
来源:oschina
链接:https://my.oschina.net/u/158589/blog/136380