What does the script /dev/null do?
Emily Wong
I was sued into another user to run screen, but I got the error Cannot open your terminal '/dev/pts/4' - please check.
I found a solution: script /dev/null and after that I can use screen. Why does this work? It creates a new pseudo terminal?
3 Answers
On UNIX, this is a virtual-file that can be written to. Data written to this file gets discarded. It is similar to the file call NUL on Windows machines.
Key point; When rooting a machine, intruders will often redirect logging to /dev/null For example, the command ln -s /dev/null .bash_history will cause the system to stop logging bash commands.
In layman's terms, it means much the same thing as black hole. Typical usage: if you don't like what I have to say, please direct your comments to /dev/null.
Think of /dev/null as a "black hole." It is the nearest equivalent to a write-only file. Everything written to it disappears forever. Attempts to read or output from it result in nothing. Nevertheless, /dev/null can be quite useful from both the command line and in scripts.
- It discards all data written to it but reports that the write operation succeeded.
- It means redirecting both standard output and error to
/dev/null - It prevents the script from displaying anything. like windows "echo off"
script /dev/null prevent any message from appearing on your screen. It supresses the messages, byt directing them to the "black hole."
Also, have a look at Why does redirecting 'script' to /dev/null/ allow 'screen' to work while su'ed as another user?
Source:Linux Dictionary
4Basically script saves all terminal dialogue into a file, when you specify /dev/null as the file all the stuff script would save into a file would be saved into the black hole.
script FILE COMMAND... is used to run COMMAND saving a copy of all input and output to FILE, while still enabling interactive input/output through your terminal.
script /dev/null COMMAND... therefore runs COMMAND, and the copy of the output goes to the file /dev/null, the bitbucket, in other words it just gets deleted; but the output still goes to your terminal as if you were not using script.
What is the purpose of this? It sounds like a pointless step which doesn't achieve anything useful – taking a copy of the output and then sending the copy nowhere. However, it does have one side-effect which is helpful.
Some commands use the isatty() function to determine whether stdin/stdout/stderr is a terminal, and they behave differently depending on whether it is or not – for example, they may output ANSI colour codes if stdout is a terminal, but not do so if it is not. That is usually what you want – if you are sending the output to a file or piping it to another program, you usually don't want ANSI escape codes in that output. However, what if you actually do? How can you get the program to send output including ANSI escape codes even when the output isn't a terminal? Some programs have a command line option or environment variable to force colour output even when stdout is not a terminal, but not all do. For programs which lack such a facility to force colour output, script /dev/null can be used as a workaround. It will allocate a pseudoterminal, pass that to the command as stdout, and then copy the output sent to that pseudoterminal to its own (non-terminal) stdout. Similarly, some programs (intended for interactive use) refuse to receive input if stdin is not a terminal, so you can't pipe input to them from another program or redirect their input from a file; script /dev/null can be used to get around this.