10.18. Finding the Parent Process - OR, "Dealing With 'script'"

I use the script command occasionally to record the output of an interactive process. It's very handy, but its output is plain text and escape sequences from colourised prompts look pretty ugly. There are two very different ways of dealing with this: you can use less -R typescript (where typescript is the output of script) which tells less to display "raw" control characters. This makes for pretty output, and works well ... unless you're posting to the web, or need plain text for some other reason. And even if you don't want plain text, it can sometimes be confusing running script because no indication is given that you're in a subshell and your output is being recorded. So I wanted a way to detect that I was working inside script. I use the following method to go to a plain text font and indicate that we're inside script:

parent="$(ps -p $PPID -o comm= )"

case ${parent} in
    local GRAY="\[\033[1;30m\]"
    local LIGHT_GRAY="\[\033[0;37m\]"
    local WHITE="\[\033[1;37m\]"
    local LIGHT_BLUE="\[\033[1;34m\]"
    local LIGHT_RED="\[\033[1;31m\]"
    local YELLOW="\[\033[1;33m\]"

PS1= # prompt with embedded colours and the $SCRIPT variable

If we're not in script, the colours are set. But if we're in script, the colour variables are empty, and the previously empty $SCRIPT now has a value in it that appears in the prompt.