Operators

Order of redirection matters

Note that stdout (fd 1) is used by default if none is specified

Eg. > is equivalent to 1>

  • The following directs both stdout (fd 1) and stderr (fd 2) to the file dirlist

ls > dirlist 2>&1 
  • While the following directs only the stdout (fd 1) to the file dirlist

    • The stderr (fd 2) still points to terminal (orginal value of stdout)

ls 2>&1 > dirlist

The reason being that the shell interprets the values from left to right.

Steps in the first example

  1. > dirlist: Redirect stdout to dirlist file

  2. 2>&1: Redirect stderr (fd 2) to stdout (&1, fd 1)

  • Since stdout is pointing to the dirlist file, this points stderr to that file too

Steps in the second example

  1. 2>&1: Redirect stderr (fd 2) to stdout (fd 1)

  • However, since stdout is currently pointing to the terminal, the stderr will point to it too

  1. > dirlist: Redirect stdout to the dirlist file

  • Overall, stderr points to the terminal, while stdout points to the dirlist file

Operators

  1. >: overwrite data in file

  • Notice that the text bye is replaced by hello

  1. >> : append data to file

  • Notice that the text hello is appended to the file without overwriting the text bye

  1. <: read from file

  1. Pipe (|): redirect stdout from one process to the stdin of another

Last updated