---
layout: page
category-page: scripts
category-title: Scripting
tags: redirect output input
author: Dario Rasic
title: Redirection
---
Output as input
To redirect a certain output of the command-line we have to use the symbol ">".
In this case, we will use a file named hello.txt, in which we want to insert
a certain output ("Sun" in this case):
echo "Sun" > hello.txt
So if we print the content of the file in our command-line, the output will be the following:
cat hello.txt
Sun
If we want to append a certain output to an existing file,
we just have to use ">>" symbols:
cat hello.txt
Sun
echo "Moon" >> hello.txt
cat hello.txt
Sun
Moon
Input as output
To redirect an input from a file for a command, the symbol "<" is used.
echo < $(cat hello.txt)
Sun
Moon
This is particularly useful when chaining commands.
Chaining (or Piping)
Chaining, also called Piping because it works with the pipe symbol "|", takes the output of a certain command-line and feeds it to another command in a direct way.
cat hello.txt | grep Mo
Moon
Simple Example
Now let's say that we want to combine those commands in a more complex operation. Let's say we want to take some contents from a certain file and put it into another file.
We want to sort the animals whose name begins with letter "d" from the file animals.txt and put it into d_animals.txt.
grep d < animals.txt > d_animals.txt
cat d_animals.txt
Deer
Dog
Dolphin
...