Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 -


command -

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

share|improve this question

migrated from Oct 9 '12 at 10:14

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
<sarcasm>Reading the documentation is so boring. For example "-v" means always verbose. pkill is a good example for that</sarcasm> – Alex Oct 8 '12 at 20:47

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.

share|improve this answer

You must log in to answer this question.

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