0
$ cd /tmp
$ echo bar > foo
$ cat foo
bar
$ cat foo | sed 's/bar/qux/'
qux
$ cat foo | sed 's/bar/qux/' > foo
$ cat foo

The last cat foo shows that the contents of the file have been erased. Why is that?

1 Answers1

2

The I/O redirection > foo is handled by the shell before executing the command. The shell opens the file foo for write, erasing previous contents and then, executes the command. In your case, cat foo | sed 's/bar/qux/' works with an empty file foo and hence, it has no effect.

unxnut
  • 6,008