redirection vs pipe

Redirection (>, <) and pipe (|), both are used to pass output of a process. However, there exists one fundamental differences between them. Even though redirection can be used to pass to stream which also can be a file, pipe can pass output of a process to another process.

For example, this command does that:

dmesg | grep selinux > temp.log
  1. executes dmesg
  2. passes output of dmesg to grep process
  3. executes grep on output of dmesg and take selinux as a pattern
  4. then redirects output of grep to a file named temp.log, if does not exists, creates it.

In fact, above-command can be typed like that:

dmesg > dmesg.out && grep selinux < dmesg.out > temp.log

Which does same job, except that it writes dmesg output to file named dmesg.out. Instead of double redirection, using pipe eases our job.