-1

I'm trying to understand how ">>" works. Does it use auto-append mode or lseek?

I've downloaded the bash source and have been trying to grep my way through to find how it works, but I've been unsuccessful. Maybe there is a better way?

4

1 回答 1

3

由于这是通过系统调用工作的,因此最简单的了解方法是使用 strace 运行 bash。您可以执行以下操作:

$ strace -o bash.log bash
$ echo hello >> test.txt
$ echo goodbye >> test.txt
$ exit
$ grep test.txt bash.log
open("test.txt", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
open("test.txt", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
write(3, "echo hello >> test.txt\necho good"..., 48) = 48

您可以通过传递给open()调用的标志看到它直接以附加模式打开。

于 2013-04-17T15:13:57.663 回答