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

原创
2013/06/08 00:23
阅读数 781

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


展开阅读全文
打赏
0
3 收藏
分享
加载中
更多评论
打赏
0 评论
3 收藏
0
分享
返回顶部
顶部