Does any magic "stdout" file exists?

linux bash shell pipe

62 观看


706 作者的声誉

This question already has an answer here:

Some utilities can not output to stdout.


util out.txt

It works. But sometimes I want to pipe the output to some other program like:

util out.txt | grep test

Does any magic "stdout" file in linux exists, so when I will replace the out.txt above, it will work redirect the data to stdout pipe?

Note: I know util out.txt && cat out.txt | grep test, so please do not post answers like this.

作者: Mvorisek 的来源 发布者: 2017 年 12 月 27 日

回应 1


185476 作者的声誉


You could use /dev/stdout. But that won't always work if a program needs to lseek(2) (or mmap(2)) it.

Usually /dev/stdout is a symlink to /proc/self/fd/1 (see proc(5)).

IIRC some version of some programs (probably GNU awk) are handling specifically the /dev/stdout filename (e.g. to be able to work without /proc/ being mounted).

A common, but not universal, convention for program arguments is to consider -, when used as a file name, to represent the stdout (or the stdin). For example, see tar(1) used with -f -.

If you write some utility, I recommend following that - convention when possible and document if stdout needs to be seekable.

Some programs are testing if stdout or stdin is a terminal (e.g. using isatty(3)) to behave differently, e.g. by using ncurses. If you write such a program, I recommend providing a program option to disable that detection.

作者: Basile Starynkevitch 发布者: 2017 年 12 月 27 日