LINFO

Standard Input Definition



Standard input, often abbreviated stdin, is the source of input data for command line programs (i.e., all-text mode programs) on Linux and other Unix-like operating systems.

Such operating systems feature the concept of standard streams of data. Each command, and therefore each process, is automatically initialized with (i.e., assigned) three data streams: one input stream, called standard input, and two output streams, called standard output and standard error. These streams consist of data in plain text (i.e., human readable alphanumeric characters) form and are considered to be special types of files.

A command is an instruction given by a user to tell a computer to do something, such as run a program. Commands are generally issued by typing them in at the command line and then pressing the ENTER key, which passes them to the shell. A shell is a program that reads commands that are typed on a keyboard and then executes (i.e., runs) them. A process is an instance of a program in execution.

This terminology can be confusing to new users. However, it is useful to become familiar with it because it is commonly employed in the documentation for Unix-like operating systems, including the online man (i.e., manual) pages, which are found on almost all such systems.

Users familiar with the C programming language, in which the Linux kernel (i.e., the core of the operating system) and many of its utilities are written, will be aware that it includes routines to perform basic operations on standard input and standard output. Examples include printf, which allows text to be sent to standard output, and scanf, which allows a program to read from standard input.

Standard input is by default any text entered from the keyboard. It is the counterpart of standard output and standard error, which are by default the display monitor.

Standard input can be redirected through the use of arguments and redirection operators so that it becomes a source other than the keyboard (just as standard output and standard error can be redirected so that they go to a destination other than the display monitor). An argument, also called a command line argument, is a file name or other data that is provided as an input to a command.

A simple example is provided by the wc command, whose default behavior is to count the number of lines, words and characters in text. Typing this command in at the command line and pressing the ENTER key to start a new line allows any text subsequently entered at the keyboard to become the standard input for wc. When the command is then executed (by simultaneously pressing the CONTROL and d keys on a new, blank line), wc counts the numbers of lines, words and characters in the typed-in text and reports the results on the display monitor (i.e., standard output), and the program is terminated.

In the event that one or more input files are provided as arguments to a program, standard input is automatically redirected to become those input files instead of the keyboard. Thus, in the following example the standard input becomes file1 and wc will print (UNIX terminology for write) the totals of lines, words and characters in file1 on the display monitor:

wc file1

Standard input can also be redirected to come from any text file in place of the keyboard by using the input redirection operator, which is represented by a leftward pointing angle bracket ( < ). Thus, the following would redirect the standard input for wc from the keyboard to the file named file1:

wc < file1

The result is the same as in the example in which file1 is used as an argument. However, the mechanism is different. This is consistent with the tenet of the Unix philosophy (which is also fundamental to Linux) that states that it is desirable that there exist more than one way to perform any task.

Instead of obtaining its input from the keyboard or from a file, a program can use the output of another program as its input. This is accomplished through the use of a pipe, which is represented by the vertical bar character. For example, the head command (which by default reads the first ten lines of text) could be used to read the first ten lines of file1 and pipe the output to wc:

head file1 | wc

Thus, wc would report the totals for the numbers of lines (which would, of course, be ten), words and characters in the first ten lines of file1.

As is the case with other commands, the standard input for head is the keyboard by default. Thus, in the absence of any arguments (e.g., file1) or redirection from another command, head would read the first ten lines of any text typed in at the keyboard and then send them to standard output (i.e., repeat them on the display screen) in the absence of any output redirection. This can easily be seen by entering the following command, then typing more than ten lines of text, each terminated by pressing the ENTER key (to notify the system of a start of a new line):

head

Each line from standard input (i.e., typed in at the keyboard) will be echoed (i.e., repeated on the screen) until ten lines have been typed in.






Created April 25, 2005. Updated December 19, 2006.
Copyright © 2005 - 2006 The Linux Information Project. All Rights Reserved.