bash源代码分析----bash --login的作用和--noprofile的作用
[root@localhost ~]# cat ~/.bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/binexport PA
[root@localhost ~]# cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
lshell="login shell will see this message"
echo $lshell
[root@localhost ~]# bash --login
login shell will see this message
[root@localhost ~]# exit
logout
[root@localhost ~]# bash --noprofile
[root@localhost ~]#
shell.c文件部分内容:
struct {
char *name;
int *value;
int type;
} long_args[] = {
{ "debug", &debugging, Int },
{ "norc", &no_rc, Int },
{ "noprofile", &no_profile, Int },
{ "rcfile", (int *)&bashrc_file, Charp},
{ "version", &do_version, Int},
{ "quiet", &quiet, Int},
{ "login", &make_login_shell, Int},
{ "nolineediting", &no_line_editing, Int},
{ "nobraceexpansion", &no_brace_expansion, Int},
{ (char *)NULL, (int *)0x0, 0 }
};
main (argc, argv, env)
if (!locally_skip_execution)
{
if (login_shell)
maybe_execute_file ("/etc/profile");
if (login_shell && !no_profile) //如果加了bash --noprofile选项则不执行下面的脚本
{
/* If we are doing the .bash_profile, then don't do the .bashrc. */
no_rc++;
if (act_like_sh)
maybe_execute_file ("~/.profile");
else
{
if (maybe_execute_file ("~/.bash_profile") == 0)
if (maybe_execute_file ("~/.bash_login") == 0)
maybe_execute_file ("~/.profile");
}
/* I turn on the restrictions afterwards because it is explictly
stated in the POSIX spec that PATH cannot be set in a restricted
shell, except in .profile. */
if (*++(argv[0]) == 'r')
{
set_var_read_only ("PATH");
restricted++;
}
}
/* If user supplied the "-login" flag, then set and invert LOGIN_SHELL. */
if (make_login_shell)
login_shell = -++login_shell;
如果加了bash --login则make_login_shell为1,login_shell为1。
lshell="login shell can see this message "
echo $lshell
[root@localhost ~]# cat /etc/profile |grep lshell
lshell="login shell can see this message "
echo $lshell
[root@localhost ~]# exit
logout
[root@localhost ~]# bash --login
login shell can see this message
login shell will see this message //这里说明login shell会执行/etc/profile和~/.bash_profile文件。(如果存在的话,具体看上面)
[root@localhost ~]# exit
logout
You have new mail in /var/spool/mail/root
[root@localhost ~]# bash
[root@localhost ~]#
[root@RCD ~]# bash --noprofile
[root@RCD ~]# exit
exit
You have new mail in /var/spool/mail/root
[root@RCD ~]# bash --noprofile --login
bash-4.1# pwd
/root
bash-4.1#
看来~/.bash_profile可以设置用户名和主机和所在的目录。
更多推荐

所有评论(0)