"cat xyz> z"如何运作?

Jua*_*ews -1 linux bash

我有三个文件x,y和z以及一些内容.

如果我运行$cat x y z > z它似乎在将x和y写入z之前截断文件z.

我期望它只是将x,y和z的内容连接成z.有人可以解释为什么不是这样吗?还请解释我收到的这个警告

$ echo "one" > x
$ echo "two" > y
$ echo "three" > z
$ cat x y z 
  one
  two
  three
$ cat x y z > z
  cat: z: input file is output file
$ cat z
  one
  two
Run Code Online (Sandbox Code Playgroud)

Mis*_*cha 5

在开始之前>z,shell 通过打开z输出并截断它来作用cat.cat通过检查stdout的inode对每个输入的inode来保护你免受无限循环(无限直到你的磁盘已满).