Understanding Commonplace I/O on Linux

If you're using Linux, you might see references to "standard I / O" or "standard input", "standard output" and "standard error". What do these terms mean?

Standard input

Standard input is a term used to describe the input that a command-based program receives. In interactive use, it usually comes from the keyboard, but as you will see later, it can also come from a file.

While the keyboard is now usually connected directly to the machine, when text terminals were more common, the standard inputs were made from the terminal keyboard, which was connected to a central minicomputer or mainframe. Modern Linux systems use terminal emulators or the system console for standard input.

Standard edition

Like standard input, standard output is where a program sends its text output. Again, this is typically a terminal emulator on modern systems, but in the past also on physical terminals, either with CRT screens or printed on paper with teleprinters.

Teletype terminals were more common than Linux's predecessor, Unix, was developed by Bell Labs in the late 1960s and early 1970s.

Related: Why Are Linux Commands So Short? The history of Linux commands

As with standard input, you can also redirect standard output to a file.

Standard bug

Standard errors are typically used for any error message that a program can generate. As with standard output, it is usually displayed on the screen, but it can also be saved to a file or to a block device such as a computer. B. be redirected / dev / null.

How to redirect input and output on Linux

One of the most powerful functions of Linux and Unix systems is the ability to redirect input and output to files and other programs.

The most common method is to send the output from one instruction to another, or "pipeline". For example, to see how many Linux commands have "sh" in their names, you can grep the output of the ls command.

ls / bin | grep & # 39; sch & # 39;

To redirect the output of a command to a file, use the > Operator. For example, to send the output of the ls command to a filename File list:

ls> file list

the >> Operator appends the output to an existing file or creates it if it does not exist. To prevent a file from being accidentally overwritten, you can set the "noclobber" option in bash:

Set noclobber

You can also have a program take input from a file using the < Operator. The cat program can print the contents of a file by taking input from the file and sending the output to standard output.

Cat <file

Of course, you can just use cat and supply the file path as an argument, but this is just an example.

You can redirect standard errors using a file descriptor or a number representing one of the forms of standard I / O. For file descriptors, 0 is standard input, 1 is standard output, and 2 is standard error. The syntax in bash is (File descriptor)>. It is useful to use the standard error. redirect / dev / null to get rid of bugs:

linux_command 2> / dev / null

You can redirect both standard output and standard input at the same time using &>which is useful when you need to send an email or forum post describing a problem with a program:

linux_command &> file

Standard I / O works everywhere

Even with the graphical environments available today, standard I / O remains important as it is still the universal interface, from the desktop to the server to the mobile device, based on ASCII text.

What is ASCII text and how is it used?

ASCII text appears cryptic, but is used in many ways on the Internet.

Continue reading

About the author

David Delony
(62 published articles)

David is a freelance writer living in the Pacific Northwest but originally from the Bay Area. He has been passionate about technology since childhood. David's interests include reading, watching quality TV shows and movies, retro games, and collecting records.

By David Delony

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!

Click here to subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *