Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

To create a tar file for a directory, the tar command with compress, verbose and file options can be typed thus:

$ tar -cvf my.tar my_directory/

But it also works to do it this way:

 $ tar cvf my.tar my_directory/

That is, without the dash (-) preceding the options. Why would you ever pass a dash (-) to the option list?

share|improve this question
    
add comment

migrated from stackoverflow.com May 22 '11 at 2:06

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

3 Answers

up vote 26 down vote accepted

There are several different patterns for options that have been used historically in UNIX applications. Several old ones, like tar, use a positional scheme:

command options arguments

as for example tar uses

tar *something*f "file operated on" *"paths of files to manipulate"*

In a first attempt to avoid the confusion, tar and a few other programs with the old flags-arguments style allowed delimiting the flags with dashes, but most of us old guys simply ignored that.

Some other commands have a more complicated command line syntax, like dd(1) which uses flags, equal signs, pathnames, arguments and a partridge in a pear tree, all with wild abandon.

In BSD and later versions of unix, this had more or less converged to single-character flags marked with '-', but this began to present a couple of problems:

  • the flags could be hard to remember
  • sometimes you actually wanted to use a name with '-'
  • and especially with GNU tools, there began to be limitations imposed by the number of possible flags. So GNU tools added GNU long options like --output.

Then Sun decided that the extra '-' was redundant and started using long-style flags with single '-'s.

And that's how it came to be the mess it is now.

share|improve this answer
4  
"and a partridge in a pear tree" Better than my dry attempt. You win. –  dmckee May 21 '11 at 23:48
    
nice historical context. –  Milktrader May 22 '11 at 0:03
    
I notice that a lot of X programs (including, say, X, from Xorg), use long-style flags with a single -. Is that from Sun? –  mattdm May 22 '11 at 2:29
    
You've also missed an important step: before inventing GNU style options with "--", Posix.2 specified to use "-W" for the same goal and this was supported by several Unix flavors. GNU getopt allows them in parallel if add "W;" to its option string, but two-hyphen style is recommended which is easier to read. –  Netch Dec 18 '13 at 11:14
    
Hm, the only place I've ever seen that is -W arguments for gcc warnings. –  Charlie Martin Dec 19 '13 at 15:44
add comment

The dash is used in order to disambiguate between an option parameter names and values. I guess it's more of a standard convention.

share|improve this answer
    
I was going to add the best-practice tag because I thought it may come down to that. –  Milktrader May 21 '11 at 23:30
1  
@Milktrader, it's a difficult question when talking about best practices. Following standard conventions is indeed best practice, but whether you want to do it in your cases it's not clear. It will depend on your scenario. If you are hacking some quick command in the shell you probably wouldn't care much about it but if you are writing a library it is important to follow well established conventions because you don't have control of how this library is going to be used by clients. In this case you should cover all possible cases and disambiguating between option names and values is important. –  Darin Dimitrov May 21 '11 at 23:33
add comment

You can give tar options in the standard unix manner tar -c -f foo -v -B file1 file2 file3 where you need the dash to differentiate between options and parameters or the file names at the end of the command line. Or you can put all the options together in the first argument, in which case the dash is optional.

Then there is ps, where you use the dashes if you're using the SysV-ish options, and leave them out if you're using BSD-ish options, just to make things more confusing.

And lets not even talk about find.

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.