[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash Question



On Thu, Jul 06, 2017 at 05:25:05PM +1000, David wrote:
> Shells do not set this variable to identify themselves.

> On jessie, 'man 1 login', states that it sets SHELL. I understand this to
> mean that 'login' exports SHELL as an environment variable to child
> processes of 'login'.
> 
> I believe 'login' sets SHELL to the value of the user's login shell, as read
> from /etc/passwd. Once. And after that it does not change.

Yes, this is correct.  The SHELL variable is used by programs like vi
that have a "shell escape" feature, so that they know which shell to
execute.  (And also X terminal emulators, etc.)  It's what shell you
*want*, not what shell you *are*.

Other login-type programs should also export SHELL, including sshd and
the various Display Managers.

If you want to know which shell you're currently in, the best command
I've found so far is:

ps -p $$

Works in Bourne family shells, csh family shells, SysV family ps, and
BSD family ps.  Of course, it's only as accurate as the output of ps,
which a clever user can spoof by overriding argv[0], or by creating
a symlink.

wooledg:~$ (exec -a tentaclesh bash)
wooledg:~$ ps -fp $$
UID        PID  PPID  C STIME TTY          TIME CMD
wooledg  19021  2244  0 07:47 pts/6    00:00:00 tentaclesh
wooledg:~$ ps -p $$
  PID TTY          TIME CMD
19021 pts/6    00:00:00 bash

wooledg:~$ ln -s /bin/bash tentaclesh
wooledg:~$ ./tentaclesh
wooledg:~$ ps -p $$
  PID TTY          TIME CMD
19029 pts/6    00:00:00 tentaclesh
wooledg:~$ ps -fp $$
UID        PID  PPID  C STIME TTY          TIME CMD
wooledg  19029  2244  0 07:51 pts/6    00:00:00 ./tentaclesh

ps's output is inherently unreliable, but "ps -p $$" with *NO* other
options gives you the best chance you will get.  Still, nothing is
perfect.


Reply to: