4

I have seen a bash script that uses script command for several times. I used man script and showed me script - make typescript of terminal session

After that i google it but i didn't see any useful thing.I have two questions. These are:

1- What's the use of script command?

2- Why command script -qc "exit 1" returns exit code 0?

1
  • 3
    It creates a "recording" of what happened in a terminal window. Try typing script, do a few things, and then type exit. Then view the file called typescript in the directory where you typed script. – Ray Apr 13 at 7:36
3

1- What's the use of script command?

Sometimes commands make more sense in the context of other commands that are meant to be used with it. If you check the SEE ALSO section of the manpage, you see:

SEE ALSO
       csh(1) (for the history mechanism), scriptreplay(1), scriptlive(1),

Checking scriptreplay:

NAME
       scriptreplay - play back typescripts, using timing information

SYNOPSIS
       scriptreplay [options] [-t] timingfile [typescript [divisor]]

DESCRIPTION
       This  program  replays a typescript, using timing information to ensure
       that output happens in the same rhythm as it originally  appeared  when
       the script was recorded.

       The  replay  simply  displays  the information again; the programs that
       were run when the typescript was being  recorded  are  not  run  again.

Checking scriptlive:

NAME
       scriptlive - re-run session typescripts, using timing information

SYNOPSIS
       scriptlive [options] [-t] timingfile [-I|-B] typescript

DESCRIPTION
       This  program  re-runs  a typescript, using stdin typescript and timing
       information to ensure that input happens in the same rhythm as it orig‐
       inally appeared when the script was recorded.

       The  session  is  executed  in  a newly created pseudoterminal with the
       user's $SHELL (or defaults to /bin/bash).

So, with script you can make a recording you can use to replay a session (like a video but with text), or re-execute it. Re-executing would be useful in cases where you want to do something in a TUI and repeat your actions with optionally the same timing.

2- Why command script -qc "exit 1" returns exit code 0?

Its main job is to record, so it makes sense to return success when it successfully recorded, even if it recorded failure. However, there's also -e/--return if you wanted the return value to be passed:

 -e, --return
        Return the exit status of the child process.  Uses the same for‐
        mat as bash termination on signal termination (i.e., exit status
        is 128 + the signal number).   The  exit  status  of  the  child
        process is always stored in the type script file too.
7

From man script

script - make typescript of terminal session

script makes a typescript of everything displayed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

A typescript is "a copy of a text or document that has been typed". So, the command basically records the session.

1
  • 1
    okay, added ;-) thx for the headsup – pLumo Apr 13 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.