This SO answer says that script adds the variable SCRIPT to the environment of the command it runs. I tried that but it doesn't seem to be working now.

This is what I have tried

ma08@IWeighHar:~/pro$ script foo.log
Script started, output log file is 'foo.log'.
ma08@IWeighHar:~/pro$ echo $SCRIPT

ma08@IWeighHar:~/pro$ exit
Script done.
ma08@IWeighHar:~/pro$ echo $SCRIPT


How to detect if the script command is already running in the current terminal session?

Due to the name of this command, googling regarding it is kind of a nightmare.

  • 4
    Looking through the source (and locally testing) it doesn't seem like util-linux's script sets this environment variable. If you're in control of the execution, then you might want to try something like SCRIPT=foo script ... to set the variable yourself. BSD script (on macOS, anyway) does set this variable.
    – muru
    Aug 8, 2022 at 2:18
  • @EduardoTrápani Thanks for the feedback. Added what I have tried as an example.
    – ma08
    Aug 8, 2022 at 4:42
  • @muru Thanks, that's a bummer. I will do that. Would be nice to have the builtin down the line.
    – ma08
    Aug 8, 2022 at 4:44
  • 2
    Did you read the comments below the answer that recommends this? They explain which versions of script set the variable.
    – Barmar
    Aug 9, 2022 at 6:45
  • Thanks @Barmar. I just saw the latest comment regarding different version of script. Good to know, thanks for pointing that to me. I guess I will have to stick to using a manual of detecting if I am using a script version without that feature.
    – ma08
    Aug 9, 2022 at 8:27

3 Answers 3


As a heuristic, you could check whether there's a process whose name is script has your terminal device opened:

if lsof -watc script "$(tty)" > /dev/null 2>&1; then
  echo I may very well be within a script session

tty returns the path of the terminal opened on stdin. Some shells set the $TTY variable to the path of their controlling terminal. See also $(ps -o tty= -p "$$") to get the controlling terminal without the /dev/ prefix.


Since the variant or version of the script utility you use does not set the SCRIPT environment variable within the shell it starts, you can do so yourself. In variants of the utility that sets this variable, it is set to the name of the output file. The following is a short bash shell function that would mimic that behaviour:

script () {
    local outfile="${1:-typescript}"
    SCRIPT=$outfile command script "$outfile"

This function takes an optional single argument which is the pathname of the output typescript file. If no argument is supplied (or it's empty), the name typescript is used as the output file. The name of the output file is also available as "$SCRIPT" in the shell that the utility starts.

You would use this function like

script myfile

or as


Alternatively, to allow an arbitrary command at the end of the command line (requires that the first argument is the typescript output file and, just like with the above variation, does not allow for giving options to the real script command):

script () {
    local outfile="${1:-typescript}"
    SCRIPT=$outfile command script "$outfile" "$@"

You could use this as above, but you could also add a command to run:

script outfile mail -s testmail user@remote.host

This will offer what you need:


# placing into Creative Commons - may not be claimed for copyright by any other individual or corporation.
# free to use and replicate

BASE=`basename "${0}" ".sh" `

if [ $# -ne 1 ] ; then  echo "\n Must provide current shell PID by referencing it with $$ on the command line.\n" ; exit 1 ; fi


ps -ef >${TMP}.ps
#username   37539    4819  0 15:47 pts/3    00:00:00 bash
#username   37572   37539  0 15:49 pts/3    00:00:00 script scriptLog.txt
#username   37573   37572  0 15:49 pts/4    00:00:00 bash -i

awk -v sPID="${MySHELL}" '{
    if( $2 == sPID ){
        print $3 ;
    } ;
}' <${TMP}.ps |
{   read sPPID
    awk -v sPPID="${sPPID}" '{
        if( $2 == sPPID ){
            n=index( $0, "script " ) ;
            if( n == 0 ){
                print "\t safe to use \"script\"." ; exit 0 ;
                COMMAND=substr( $0, n ) ;
                printf("BUSY [%s]:  %s\n", sPPID, COMMAND ) ; exit 1 ;
            } ; 
        } ;
    }' <${TMP}.ps

exit 0
exit 0
exit 0

If script is already logging the current shell, it will return RC=1 and report like this:

BUSY [37572]:  script scriptLog.txt

Otherwise, RC=0 and will report like this:

        safe to use "script".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .