Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I feel shy to ask this question, but if I don't I will never know, so here I am giving it a shot:

I notice most shell commands use "-" for options, but I also noticed some commands do not have it. For example, to archive files in a given direct, the command is:

tar czvf allmyfiles.tar.gz *

However, to extract an archive, the command I learned, is:

tar -zxvf allmyfiles.tar.gz

Looking at both examples above, is there any significance whether to include the hyphen or not?

share|improve this question
    
tar -czvf allmyfiles.tar.gz * will work too –  Zypher May 14 '10 at 4:55

3 Answers 3

The commands 'tar' and 'ar' are very, very old and pre-date the convention using dashes.

share|improve this answer
    
On a related note, for some programs options used to be prefixed by + as well. dig for example. –  Dan Andreatta May 25 '10 at 8:41

Most modern programs use getopt for parsing their arguments:

... the original authors picked out of the variations support for single character options, multiple options specified together, and options with arguments (-a arg or -aarg), all controllable by an option string.

But as always, there are some programs which do things their own way regardless (dd is a good example).

Update: BSD programs tend to use options without hyphens - for example, the manpage of ps states:

This version of ps accepts several kinds of options:

  1. UNIX options, which may be grouped and must be preceded by a dash.
  2. BSD options, which may be grouped and must not be used with a dash.
  3. GNU long options, which are preceded by two dashes.
share|improve this answer

With tar, it (usually) makes no difference. There is no rule to say whether or not all commands will follow this convention or not, but the general guideline is command -arguments.

share|improve this answer
    
In tar there is a difference. When using old-style option (without -) you have to give all options first and after that all the arguments. When using short-style (with -) all arguments have to follow their associated options. So "tar czfv allmyfiles.tar.gz *" creates the archive "allmyfiles.tar.gz" but "tar -czfv allmyfiles.tar.gz *" creates the archive "v" (and gives an error if allmyfiles.tar.gz doesn't exist, as it is to be included into v). –  Florian Diesch May 14 '10 at 21:37

Your Answer

 
discard

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