Try putting the logic into your ~/.profile
(if using the deflogin on
setting) or ~/.mkshrc
(or similar, depending on your shell) file. Something like this:
if test -n "$STY"; then
# we are inside GNU screen
screenpid=${STY%%.*}
screenname=${STY#*.}
# do your magic
fi
In this scenario, $screenname
will contain either the name passed to the -S
option of GNU screen, or something like ttyp0.shorthostname
or pts-9.shorthostname
(depending on the OS) if -S
was not used.