How can I tell whether I'm in a screen?

后端 未结 9 1706
悲哀的现实
悲哀的现实 2020-12-07 18:11

When using screen in linux, how can I tell if I\'m in a screen or not? I could do exit and I\'ll exit a screen if I was in one, but if I wasn\'t, then I\'ll end

9条回答
  •  执念已碎
    2020-12-07 18:51

    Since all of the other methods here rely on environment variables (which can simply be overridden) or the command character for screen (which can also be overridden), the most foolproof way to check would be to list all the ancestors of the current process.

    pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | grep screen | wc -l
    

    If it prints 1, then the current process you're running has an ancestor with the word 'screen' in the executable's name, otherwise there wasn't.

    A more facile visible inspection might be obtained from:

    pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | less
    

提交回复
热议问题