为什么我不应该让每个单独的 shell 都运行一个登录 shell?

为什么我不应该让每个单独的 shell 都运行一个登录 shell?

许多人告诉我,我不应该将我使用的每个 shell 都设置为登录 shell。无论是通过 TTY,还是通过运行 bash 的 Xorg 终端应用程序……有人能解释一下这种逻辑背后的潜在原因,或者帮我揭穿它吗?谢谢

答案1

首先想到的原因是,您通常应该遵循公认的做法。shell 的调用方式决定了它读取的配置文件。来自man bash

   When  bash  is invoked as an interactive login shell,
   or  as  a  non-interactive  shell  with  the  --login
   option, it first reads and executes commands from the
   file /etc/profile, if that file exists.  After  read‐
   ing   that   file,   it  looks  for  ~/.bash_profile,
   ~/.bash_login, and ~/.profile,  in  that  order,  and
   reads  and  executes commands from the first one that
   exists and is readable.  The --noprofile  option  may
   be  used  when  the  shell is started to inhibit this
   behavior.

   When a login shell exits,  bash  reads  and  executes
   commands from the file ~/.bash_logout, if it exists.

   When  an  interactive shell that is not a login shell
   is started, bash reads  and  executes  commands  from
   /etc/bash.bashrc and ~/.bashrc, if these files exist.
   This may be inhibited by  using  the  --norc  option.
   The  --rcfile file option will force bash to read and
   execute    commands    from    file    instead     of
   /etc/bash.bashrc and ~/.bashrc.

大多数发行版会放置全局别名、变量和 shell 选项,/etc/bash.bashrc因为它们被配置为使大多数 shell(终端等)交互式,无需登录shell。因此,如果你切换到使用登录shell,您将丢失所有这些设置。如果某些全局变量未正确设置等,这可能会导致后续问题。

此外,默认设置是在您登录以及此后的非登录 shell。这样可以很容易地在登录 shell 的设置文件中设置环境变量或应该执行一次的简单作业(例如~/.profile),优点是这些只会在您登录时运行一次。如果您全部你的shell登录shell那么这些命令将被执行多次,这是i)不优雅的,并且ii)如果这些命令占用大量资源,则会降低你的系统速度。

最后,如果用户通过 ssh 登录,并且实际在场并打开终端,通常需要设置不同的环境。如果将所有 shell 都设为登录 shell,您将无法进行区分。

现在,情况不会那么糟糕,因为如果我理解正确的话,OSX 的终端应用程序默认启动登录 shell。但是,OSX 就是这样设计的,所以我提到的潜在问题可能已经得到解决。如果您自己修改 Linux 设置,情况就不会如此。

相关内容