loader

Input, Output and Error Redirections

Based on the concept of ANSI, input/output streams called standard output and standard input, everything that runs in a shell can communicate in three ways – it can receive inputs from standard input, it can output results and information to standard output, and it can report errors to a separate output.

In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD).

Your screen also has a File Descriptor. When a program is executed, the output is sent to File Descriptor of the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed

By default, your script will open communication with three files that it can use. Standard input is going to be connected to a keyboard; your script is going to accept information from the keyboard unless you change it to something else, such as some other file or output of another script or application.

Standard output is, by default, set to the console or the screen you are running your script from. In some circumstances, we will also call a screen connected physically to your server a console.

There is also standard error, which also defaults to the screen but is a separate stream of data; if we output something to standard error, it will be displayed in a way that looks exactly the same as standard output, but that can be redirected if we need to.

 

The reason why there are two separate streams handling output is simple – we usually want to have some data as the result of our script, but we do not want errors to be part of it. In this case, we can redirect data into some file and redirect errors to the screen, or even to another file, and then deal with them later.

For now, just remember the following:

  • Standard input is file descriptor number 0.
  • Standard output is file descriptor number 1.
  • Standard error is file descriptor number 2.
  • The ‘<‘ symbol is used for input(STDIN) redirection
  • The ‘>‘ symbol is used for output (STDOUT) redirection.
  • If you do not want a file to be overwritten but want to add more content to an existing file, then you should use >>
  • &> In the Bash environment, this means that we want to redirect both standard error and output to the same file.
  • | Redirect standard output to another command (pipe)

 

Examples:

In first example we actually want to output the result of errors in a particular file, we can do that by using 2> errorfilename, and the script will write its errors into a file called errorfilename. 

In second example we redirect both standard error and output into a file called outputfile. The result redirect into outputfile and the error redirect to errorfile.

 

				
					amir@server:~$bash hello_world.sh &> outputfile

				
			
				
					amir@server:~$bash hello_world.sh 1> outputfile  2>errorfile
				
			

Leave a Reply

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