1

I have a mainly unixoid software that usually writes to a file that is specified as a parameter.

Now I want to add the ability to write to stdout instead. But to write to a file should still be the standard behavior.

I think I saw shell commands like

command --outfile -

or

command -

Is that a common practise? Unfortunately I do not recall the commands that worked like that.

3 Answers 3

3

A common method is to write to stdout when the last argument in the parameter list is a hyphen (or in some cases, a double-hyphen).

If you are creating a program, you can go an extra step and use an explicit -stdout parameter for printing to standard output. Since the - syntax is not an official standard, I would suggest that it is better to err on the side of readability.

1

The hyphen is used in various *nix commands to indicate either stdin or stdout, depending on what context it's used in. It is fairly common but by no means universal, so you should check the documentation for a specific command before using it.

1
  • <sarcasm>Reading the documentation is so boring. For example "-v" means always verbose. pkill is a good example for that</sarcasm>
    – Alex
    Oct 8, 2012 at 20:47
1

Common practice (an example is gzip) is to write to stdout if either a file is not specified (assuming you can determine that) or a hyphen is given as the last option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .