[sudo-users] Is there a way to increment SHLVL when using sudo?

David Ledger david.ledger at ivdcs.co.uk
Mon Feb 6 10:08:00 MST 2017

On 6 Feb 2017, at 15:18, Leroy Tennison wrote:

> I'm trying to tell how "deep" I am in levels of shell. I realize this 
> is not totally possible unless ssh could increment SHLVL as well (and 
> possibly other programs I'm not aware of). However, I'm thinking this 
> would at least tell me how many levels of shell I have on the current 
> system.
> ____________________________________________________________
> sudo-users mailing list <sudo-users at sudo.ws>
> For list information, options, or to unsubscribe, visit:
> https://www.sudo.ws/mailman/listinfo/sudo-users

Manually it’s easy, but it can be scripted with awk if it’s not a 
one-off need. The shell variable $$ should contain the current PID. A 
’ps’ listing of that process with the correct argument for your 
system and version of ‘ps’ will show you the Parent PID. Grab that 
and repeat until you know what you want to know.

A simple script using awk on the o/p of ps can loop until it gets to PID 
0, or you can pull an entire ps listing into an awk array and walk that 
array till you know. Having a ash background I know awk better than the 
bas extension, but you may be able to do it in bash. Perl will certainly 
do it.

‘ps’ is generally non-portable, so the chances of the script I have 
working on your system is small.


More information about the sudo-users mailing list