If you are going to use bash shell scripting to create programs that will manipulate computer data, then it is useful to control what data is input to a command, control where the data is output, and also control where error messages are output.
In the bash shell, command input and command output can be manipulated. For every command that can be used in bash there are three file descriptors: standard input (stdin), standard output (stdout), and standard error (stderr).
• Standard input – is the information that is passed to a command. The standard input information can be input by the user through the blinking prompt, or it can be passed to a command from a file or program. Standard input is also represented by the number 0 (see below)
• Standard output – is the data output after a command has executed. Standard output is also represented by the number 1 (see below)
• Standard error – are any error messages that may have been generated by a command. Standard error is also represented by the number 2 (see below)
In bash, standard input, standard output and standard error messages are manipulated by redirect command characters. Redirects control how data is input and output from files to commands and vice versa:
- The output redirector (> or 1>) redirects standard output to a file instead of the screen. Example:
$ ls -l /var > mydirectory.txt
$ ls -l /var 1> mydirectory.txt
- The input redirector (< or 0<) redirects standard input from a file. Example:
$ ls -l < /var
$ ls -l 0< /var
$ cat < mydirectory.txt
$ cat 0< mydirectory.txt
- The append redirector (>> or 1>>) appends standard output to the end of a file instead of rewriting it. Example:
$ ls -l Documents > myfiles.txt
$ ls -l Downloads >> myfiles.txt
$ ls -l Pictures 1>> myfiles.txt
- The output redirector with a 2 (2>) redirects standard error to a file. Example:
$ ls -l Pixxtures 2> errors.txt
- The output redirector with a 1 and a 2 (1> 2>) can be use to redirect standard output and standard error to two different files. The number one in the first redirector is not necessary since a greater-than sign by itself implies the number one, standard output. Example:
$ ls -l Documents Pixxtures > good.txt 2> errors.txt
The pipe is a special command character (|) which can take the standard output (stdout) from one command and make it the standard input (stdin) for another command. In the example below, the command ls -l has been issued to list the contents of the /etc directory, but before the output can be sent to the screen, it is piped (|) and turned into standard input for the more command to process. The more command takes the input and executes its program, which processes the information and outputs it to the display it one screen at a time.
$ ls -l /etc | more
In this next example, the pipe is used to take the data output from the ls -l /etc command and send it as input to the grep command to search for a specific lines of text that have the text “firefox” in them.
$ ls -l /etc | grep “firefox”
Pipes are very useful because they allow you to send information from one command to another in sequence. Pipes and redirectors can be used together very powerfully to control how data is input and output.
In this tutorial, I demonstrate using redirector characters to control standard input, output, and error
In this video, I demonstrate how to use of the pipe special character and the
grep command, to filter output to find specific lines of content