$SHLVL starts at 2 in xfce4-terminal

I’m using XFCE desktop and xfce4-terminal with fish as my shell. This is a really minor thing but noticeable because my prompt shows my $SHLVL which is useful. But when I start xfce4-terminal, $SHLVL starts at 2 instead of 1. Any idea what could cause this?

It must be something about my DE or terminal settings, since it properly starts at 1 when I log in to a non-graphical TTY. I think it must be something about my terminal settings because if I run fish -c 'echo $SHLVL > /tmp/shlvl' from the Application Finder, it outputs 1. There is an xfce4-terminal preference for running the command as a login shell, but that being on or off doesn’t make a difference to the starting $SHLVL.

try

pstree $PPID

might give you a clue.

That just shows fish at the top level. I also checked it with ps axf and it looks pretty normal to me, with fish being a child of /run/current-system/sw/bin/xfce4-terminal and no other parents above that.
However, I had a look at what exactly /run/current-system/sw/bin/xfce4-terminal is. It’s a symlink to /nix/store/zgpiyprj40n2lvnzgsdy6c4qfbmjs59y-xfce4-terminal-0.8.10/bin/xfce4-terminal which appears to be a wrapper script:

#! /nix/store/wv35g5lff84rray15zlzarcqi9fxzz84-bash-4.4-p23/bin/bash -e
export GIO_EXTRA_MODULES='/nix/store/216dss95nnivphn0rhjyf1vgb8fm1ax2-dconf-0.40.0-lib/lib/gio/modules'${GIO_EXTRA_MODULES:+':'}$GIO_EXTRA_MODULES
export GIO_EXTRA_MODULES='/nix/store/xjzizyzk16sszb8pxhvy09gpkrxsgklz-xfconf-4.16.0/lib/gio/modules'${GIO_EXTRA_MODULES:+':'}$GIO_EXTRA_MODULES
export GIO_EXTRA_MODULES='/nix/store/216dss95nnivphn0rhjyf1vgb8fm1ax2-dconf-0.40.0-lib/lib/gio/modules'${GIO_EXTRA_MODULES:+':'}$GIO_EXTRA_MODULES
export GIO_EXTRA_MODULES='/nix/store/xjzizyzk16sszb8pxhvy09gpkrxsgklz-xfconf-4.16.0/lib/gio/modules'${GIO_EXTRA_MODULES:+':'}$GIO_EXTRA_MODULES
export GDK_PIXBUF_MODULE_FILE='/nix/store/iigwdd859z267pbkwd00v6nd8v3gkdzw-librsvg-2.50.5/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache'
export XDG_DATA_DIRS='/nix/store/bsc3xi2hrhrkd40vyfb078kx60jsbldx-gtk+3-3.24.27/share:/nix/store/8kd8w81gwn25h1zjzqq5qwiqjz58841a-hicolor-icon-theme-0.17/share:/nix/store/9vlwm4jcfdhs2wdxsk4g3ijf7wiffshp-cups-2.3.3/share:/nix/store/pc8qyfvx43gr4g7n0k9dbp3kkdi33qfs-libxfce4ui-4.16.0/share'${XDG_DATA_DIRS:+':'}$XDG_DATA_DIRS
export XDG_DATA_DIRS='/nix/store/bsc3xi2hrhrkd40vyfb078kx60jsbldx-gtk+3-3.24.27/share/gsettings-schemas/gtk+3-3.24.27:/nix/store/i5jfdj3bjwms7a2niqqpdg578l3zszph-gsettings-desktop-schemas-40.0/share/gsettings-schemas/gsettings-desktop-schemas-40.0'${XDG_DATA_DIRS:+':'}$XDG_DATA_DIRS
export XDG_DATA_DIRS='/nix/store/zgpiyprj40n2lvnzgsdy6c4qfbmjs59y-xfce4-terminal-0.8.10/share'${XDG_DATA_DIRS:+':'}$XDG_DATA_DIRS
exec -a "$0" "/nix/store/zgpiyprj40n2lvnzgsdy6c4qfbmjs59y-xfce4-terminal-0.8.10/bin/.xfce4-terminal-wrapped"  "$@"

where /nix/store/zgpiyprj40n2lvnzgsdy6c4qfbmjs59y-xfce4-terminal-0.8.10/bin/.xfce4-terminal-wrapped appears to be the actual xfce4-terminal binary. Maybe the fact that the terminal is launched through a bash script causes the nested $SHLVL? I would have thought exec would take care of that but I don’t know exactly what the rules are for incrementing $SHLVL. I’m also not sure how I would go about fixing this.

1 Like

I managed to find a good workaround for this that covers any situation where the terminal app might be launched from another shell. I set the xfce4-terminal “Custom command” option to the path to a bash script called fish-terminal-launcher which has:

#!/usr/bin/env bash
SHLVL=0 exec fish -i

So whenever a terminal window is opened, it launches fish with SHLVL explicitly set to zero in its parent env, so it gets set to one by fish and works normally from then on.