Difference between revisions of "Input and Output"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
(Created page with 'Almost all commands produces the output to screen or take input from the keyboard, but in Linux it is possible to send output to a file or to read input from a file. Each shell c…')
 
 
(7 intermediate revisions by one other user not shown)
Line 1: Line 1:
Almost all commands produces the output to screen or take input from the keyboard, but in Linux it is possible to send output to a file or to read input from a file. Each shell command has its own input and outputs. Before  a  command is executed, its input and output may be redirected using a special notation interpreted by the shell. For example, sending output of date command to a file instead of to the screen. Changing the default path of input or output is called redirection.
+
{{navigation
 +
|previous=Chapter 6: Shell Redirection
 +
|next=Standard input}}
 +
 
 +
Almost all commands produce the output to screen or take input from the keyboard, but in Linux it is possible to send output to a file or to read input from a file. Each shell command has its own input and outputs. Before  a  command is executed, its input and output may be redirected using a special notation interpreted by the shell. For example, sending output of date command to a file instead of to the screen. Changing the default path of input or output is called redirection.
 +
[[File:Shell-input-output.png|center|Shell Input Output]]
 +
 
 +
* In Linux everything is a file.
 +
* Your hardware is also a file:
 +
** 0 - Input - Keyboard (stdin)
 +
** 1 - Output - Screen (stdout)
 +
** 2 - Error - Screen (stderr)
 +
* The above three numbers are standard POSIX numbers and also known as file descriptors (FD). Every Linux command at least open the above streams to talk with users or other system programs.
 +
Integer value
 +
{| style="cellpadding="20" cellspacing="0" border="1"  width="100%"
 +
! style="background:#ffdead;"| Standard File
 +
! style="background:#ffdead;"| File Descriptor Number
 +
! style="background:#ffdead;"| Meaning
 +
! style="background:#ffdead;"| Example (type at shell prompt)
 +
|-
 +
| stdin
 +
| 0
 +
| Read input from a file (the default is keyboard)
 +
| <br/><code>cat < filename</code><br/>&nbsp;
 +
|-
 +
| stdout
 +
| 1
 +
| Send data to a file (the default is screen).
 +
| <br/><code>date > output.txt<br/>cat output.txt</code><br/>&nbsp;
 +
|-
 +
| stderr
 +
| 2
 +
| Send all error messages to a file (the default is screen).
 +
|<br/><code>rm /tmp/4815162342.txt 2>error.txt<br/>cat error.txt</code><br/>&nbsp;
 +
|}
 +
You can manipulate the final result by redirecting input and output.
 +
 
 +
[[Category:Redirection]][[Category:Commands]]
 +
 
 +
{{navigation
 +
|previous=Chapter 6: Shell Redirection
 +
|next=Standard input}}

Latest revision as of 23:01, 24 January 2011

← Chapter 6: Shell RedirectionHomeStandard input →

Almost all commands produce the output to screen or take input from the keyboard, but in Linux it is possible to send output to a file or to read input from a file. Each shell command has its own input and outputs. Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. For example, sending output of date command to a file instead of to the screen. Changing the default path of input or output is called redirection.

Shell Input Output
  • In Linux everything is a file.
  • Your hardware is also a file:
    • 0 - Input - Keyboard (stdin)
    • 1 - Output - Screen (stdout)
    • 2 - Error - Screen (stderr)
  • The above three numbers are standard POSIX numbers and also known as file descriptors (FD). Every Linux command at least open the above streams to talk with users or other system programs.

Integer value

Standard File File Descriptor Number Meaning Example (type at shell prompt)
stdin 0 Read input from a file (the default is keyboard)
cat < filename
 
stdout 1 Send data to a file (the default is screen).
date > output.txt
cat output.txt

 
stderr 2 Send all error messages to a file (the default is screen).
rm /tmp/4815162342.txt 2>error.txt
cat error.txt

 

You can manipulate the final result by redirecting input and output.

← Chapter 6: Shell RedirectionHomeStandard input →