What is common between environments within a shell terminal session?

What is common between environments within a shell terminal session?

I have a custom shell script that runs each time a user logs in or identity is assumed, its been placed in /etc/profile.d and performs some basic env variable operations. Recently I added some code so that if screen is running it will reattach it without needing me to type anything. There are some problems however. If I log-in as root, and su – to another user, the code runs a second time. Is there a variable I can set when the code runs the first time that will prevent a second run of the code?
I thought to write something to the disk but then I dont want to prevent the code from running if I begin a new terminal session. Here is the code in question. It first attempts to reattach – if unsuccessful because its already attached (as it might be on an interruped session) it will ‘take’ the session back.
screen -r

if [ -z “$STY” ]; then
exec screen -dR

Ultimately this bug prevents me from substituting user to another user because as soon as I do so, it grabs the screen session and puts me right back where I started. Pretty frustrating


Solution 1:

The ${PPID} of the shell you get when you su will be the su command. So the output of

ps -o command= $PPID

will begin with the letters su, so test for this.

Solution 2:

I think you can get this right if you read the following post (and the man for your favorite shell)
Question about login vs profile

Related:  passing bash array elements to awk regex inside loop