[sf-lug] shell, man(1) [was: path name expansion]

Akkana Peck akkana at shallowsky.com
Sat Aug 20 12:14:28 PDT 2016


jim writes:
> Tne dash shell does not set the SHELL or
> the SHLVL variables. I tried to create
> an if-fi structure in ~/.profile to
> force the change but failed.

csh and tcsh don't set it either.

These days I'm using zsh. SHELL is set to /usr/bin/zsh, so that's
fine, but SHLVL is set to 3 in the xterm window I started from my
X/openbox autostart file, and 2 in the xterm windows I started from
my openbox menu after X was already running. Not very useful. I have
    XTerm*loginShell: true
in .Xdefaults, so all these xterm shells started as login shells
(their names in ps are -zsh and they ran .zlogin and .zprofile),
yet their SHLVL doesn't reflect that.

But I have another way of reminding myself about shell levels,
which I borrowed from someone else many years ago when I used csh.
In .zprofile (which is run in login shells *after* .zshrc), I have

    export primes=''

Then in .zshrc, when I set my prompt, I have (this is a simplified
version, I actually do some checks on host and username too):

    PS1='%F{blue}<'$(hostname)$primes$'>- %f%k'
    export primes=${primes}\'

So in my regular login shells (anything that runs .zprofile),
my prompt looks like this (on a machine named host), in blue:

<host>- 

But if I run a subshell, the prompt becomes

<host'>- 

If I run another subshell inside that,

<host''>- 

and so on. My .cshrc and .login have similar code, except my
csh/tcsh prompt is (host)- and is a different color, and similarly
for other shells and root shells. So my prompt tells me what host
I'm on, which shell I'm running, how many levels deep I am, and
whether I'm root.

(The .zshrc code is a little confusing, I know, because ' is used for
quoting and also for the prime. The \' in the export primes line is
where it appends the single ' character; all other ' are just quoting.)

Of course, I could export SHLVL=0 in .zprofile. But it's handy
to show the shell level in the prompt, especially when I'm recording
output with script (as in /usr/bin/script, not shell script):
the ' in the prompt reminds me I'm still in a subshell and haven't
ended the script session, which I might otherwise forget.

        ...Akkana




More information about the sf-lug mailing list