bash: non-login shell和login shell的区别

♀尐吖头ヾ 提交于 2020-04-08 13:01:55

详细介绍可以察看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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!