0

Some utilities can not output to stdout.

Example

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
  • 3,290
  • 2
  • 18
  • 53
  • 2
    Does your utility comply with POSIX utility syntax guidelines? If so, it will recognize `-` to mean stdin or stdout, as appropriate for the position. – Charles Duffy Dec 27 '17 at 16:41
  • I believe [this](https://stackoverflow.com/questions/7756609/pass-stdout-as-file-name-for-command-line-util-answer-use-named-pipes) is answered here. – AndrejH Dec 27 '17 at 16:41
  • 2
    *Why* your program doesn't natively support stdout, too, is important. As Basile says, some programs jump around in their output file while writing them, rather than doing a single front-to-back pass. In that case, streaming data directly to stdout without buffering it in a file beforehand is literally impossible. – Charles Duffy Dec 27 '17 at 16:47

1 Answers1

3

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
  • 223,805
  • 18
  • 296
  • 547