Unix / Linux Shell Scripting- INPUT & OUTPUT

Output Redirection

The ‘>’ symbol is used for output (STDOUT) redirection.

>Output Redirection

ls -al > listings

Here the output of command ls -al is re-directed to file “listings” instead of your screen.


Note: Use the correct file name while redirecting command output to a file. If there is an existing file with the same name, the redirected command will delete the contents of that file, and then it may be overwritten.”

If you do not want a file to be overwritten but want to add more content to an existing file, then you should use the’>>’ operator.


You can redirect standard output, to not just files, but also devices!

$ cat music.mp3 > /dev/audio

The cat command reads the file music.mp3 and sends the output to /dev/audio which is the audio device. If the sound configurations in your PC are correct, this command will play the file music.mp3.

Input redirection

The ‘<‘ symbol is used for input(STDIN) redirection.

<Input redirection

Example: The mail program in Linux can help you send emails from the Terminal.

You can type the contents of the email using the standard device keyboard. But if you want to attach a File to an email you can use the input redirection operator in the following format.

Mail -s “Subject” to-address < Filename


This would attach the file with the email, and it would be sent to the recipient.

The above examples were simple. Let’s look at some advanced re-direction techniques which make use of File Descriptors.


Leave a Comment