1

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?

1
  • tar -czvf allmyfiles.tar.gz * will work too
    – Zypher
    May 14, 2010 at 4:55

3 Answers 3

2

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

1
  • On a related note, for some programs options used to be prefixed by + as well. dig for example. May 25, 2010 at 8:41
2

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.
0

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.

1
  • 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). May 14, 2010 at 21:37

You must log in to answer this question.