In general, a command (a program): - Gets data to process fromstandard input or
stdin (default: keyboard). - Returns processed data tostandard output or
stdout (default: screen). - If program execution causes errors, error messages are sent to standard error or
stderr (default: screen).
These three are files, and are always open. As all open files, they are assigned to a file descriptor (an integer). File descriptors for stdin , stdout and stderr .File | File descriptor |
---|
/dev/stdin or /dev/fd/0 | 0 | /dev/stdout or /dev/fd/1 | 1 | /dev/stderr or /dev/fd/2 | 2 | 48Redirections- Redirection
- Capturing output from a file or a command (or program or script) and sending it as input to another file or command (or program or script).
| - (pipe) redirects standard output of a program as standard input of another.
> - redirects standard output to a file (and overwrites the file if it already exists).
< - uses a file as standard input.
>> - Appends standard output to a file.
< Input_data_file program1 | program2 > Output_data_file
A "filter" is a program which reads data from standard input, processes it in some way, and sends the processed data to standard output. 49Redirections Examples2> file - Direct standard error to file
2>> file - Append standard error to file.
2>&1 - Print standard error messages to standard output.
1>&2 - Print standard output messages to standard error.
50Building Command Lines From Standard Inputxargs - Build and execute command lines from standard input.
Read remaining arguments from standard input instead of specifying them and execute the command. cd /bin
ls | xargs whatis |