What is exact difference between piping and redirection?
Where should we use piping and where should we use redirection?
How they internall
Basically redirection and piping are a few ways among many to achieve Inter Process communication in Unix.
ls > FileName
ls | grep $myName
It works on simple data sharing, such as producer and consumer.
Property Comparison: Piping is always uni-directional while redirection could be used to redirecting input as well as output.
ls > grep myFileName [ Redirecting output of first command to later one ]
sort < fileName.txt [ Redirecting fileName.txt file as an input to command sort ]
One can also write below to use bi-directional redirect in single statement.
sort < fileName.txt > sortNewFile.txt
While Piping, it is always output of first command supplied to the later one and that to simulanoeously.
ls | grep myName | awk '{ print $NF }' [ multiple piping in a single statement ]
Note 1: command > fileName . If there is a command named fileName, that would make using redirection a lot harder and more error prone. One must check first, whether there's a command named like destination file.
Other ways to achieve IPC in Unix system are: