A few practical examples:
An application sending its output to stdout typically implies that the application works just fine with any type of recipient, it just sends a stream of data suitable also for a pipe or a socket. This is the very essence of Unix pipelines, so if there is any rule of thumb then this one is probably the best candidate.
On the other hand, though, a -o <output-file-name>
option is typically preferred if the application rather needs to seek over its output.
Of course you can lseek(2)
over stdout too (if it is indeed seekable), but that's conventionally unexpected behavior.
Naturally the same holds for input as well.
Redirecting from/to a file requires a shell. A dedicated option argument allows for not requiring a shell.
Redirecting from/to a file enables easy sharing of that file's position (if it is seekable) or that pipe/socket's connection among several applications, which can be a useful feat in shell scripting, such as in:
{
echo hello world
head -c 10
seq 10 | awk '{print "amazing text-manipulation of", $0}'
dd bs=1024 count=1 seek=32
echo farewell
} < /dev/random > recipient-file
Relying only on a -o recipient-file
option (or -i /dev/random
) would not allow that (so easily).
In some cases the opposite of the above may also hold: a -o <output-file-name>
option allows for making that application's output not pollute stdout while sharing it with other applications, such as in:
strace -o prog.strace output-producer > prog.output
The same holds for input, for having an application not consume a shared stdin.
unwanted-consumer -i /dev/null desired-consumer
Relying only on stdin/stdout would require an additional layer of shell, like in:
strace > prog.strace sh -c 'output-producer > prog.output'
or for stdin:
unwanted-consumer 3<&0 < /dev/null sh -c 'desired-consumer <&3'
Apart from compactness/cleanliness of the command, and apart from requiring a shell at all, invoking a shell involves a somewhat different environment and you might need to adjust that for output-producer
or desired-consumer
, increasing the overall burden.
stdout
by default and add an optional--output, -o
option?