Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Oct 9 '12 at 10:14

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

add comment

3 Answers 3

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.