0

I found both git --version and git version are okay. They will show me the same output. However, if I try some other command, say, ls -l and ls l, only the first works.

I'd like to know how the arguments work in command line. When and where the dash or double dash before flag/arguments are necessary? Or I might be wrong with some concept when using the shell.

Thanks!

wking
  • 1,293
  • 1
  • 16
  • 27

2 Answers2

1

This is command dependent. There are some common commands like git or tar who have optional dashes. Most do not. You really have to read the man page to see what the command expects.

Tony Kurc
  • 33
  • 5
  • While reading the man pages is good advice, dashes are _not_ optional in `git` — options `--version` and `--help` happen to be _also implemented as subcommands_, presumably because they act like self-contained commands (require no operands). By contrast, `tar` stems from the early days of Unix, where some utilities used the now-deprecated, dashless 'bundled' syntax for options. Short of reading the specific man page, knowing a utilty's heritage usually implies its style of syntax. – mklement0 Dec 12 '14 at 04:30
0

There are basically two styles for providing arguments on the command line.

The GNU style is characterized by characterized by options that look like this (-v) or like this (--verbose). The single dash sets of "short" options and the double-dash sets off "long" options. Not every short option will have a corresponding long option, or vice-versa. This syntax permits short options that do not take option arguments to be combined (for example, ls -al is equivalent to ls -a -l).

In the BSD style, one can have an option that looks like this: java -version where -version a single option, which may or may not take an argument. This style has evolved into one where complex commands have many subcommands, typically referred to as "verbs." This style is used by Apple in OSX, for example launchctl unload /path/to/some.plist ("unload" is the verb).

Hopefully this information will help you read the documentation as you go. You can find the options, and what they do, for any command, by executing, for example man ls. Note that, in some cases, there is more than one manpage for a given name. In this case, you can provide the section of the manual in which you would like to do the look up, eg man 1 crontab to see how to use the program that edits users crontabs, and man 5 crontab to see the format of an entry in these tables.

Understanding these families is useful. The authors of these commands do not want to reimplement option parsing, so they use one of the common libraries for performing this task. Thus, if you encounter a new command, once you know what family it falls into, you will have an easier time understanding the manpage.

xcah
  • 143
  • 3
  • Knowing a utility's heritage is indeed useful, but your attribution of styles to platforms is incorrect: the `-` style is definitely not GNU-specific; it's mandated by POSIX and all Unixes have utilities that support it, including BSD-derived systems (some very early utilities also accept a 'bundled' option string not preceded by a dash). GNU pioneered `--`, but even some BSD utilities, such as `grep`, use that style now. `java` is a bad example in that it's multi-platform by design, certainly not specific to BSD. – mklement0 Dec 12 '14 at 04:28
  • OSX _does_ have utilities that also use `-` option syntax, similar to Java, but they're again not related to BSD - presumably, they have NeXTSTEP heritage (you can tell the difference by looking at the footer of the man page: if it says 'Darwin' or 'Mac OS X', they're NOT BSD-derived). `launchctl` is Darwin-(OSX-)specific, and has nothing to do with BSD. The example you state does not even use options, but _subcommands_ - the use of which is again not specific to one platform - `git`, for instance, has subcommands too. – mklement0 Dec 12 '14 at 04:29
  • True that what I called the "GNU style" is not specific to GNU but GNU supports it religiously and getopt is implemented pretty much everywhere. The post you link is impressive, but it's also quite dense, so a simpler, if not totally accurate decription seemed best. Regarding Java, Bill Joy was a/the key person in the original Berkeley System Distribution. OSX systems (especially with MacPorts) have utilities from everywhere, however, the stuff Apple has written for itself uses the style I alluded to (and note that almost every GUI administration tool on OSX has a command line equivalent). – xcah Dec 12 '14 at 08:22
  • "Regarding Java, Bill Joy was a/the key person in the original Berkeley System Distribution." - that doesn't make `java` a BSD utility; in fact, it _doesn't even come with BSD distributions_ such as FreeBSD and PCBSD. Yes, many (but not all) _Apple-specific_ utilities use the `-` style, but they are _non_-BSD utilities (often from a NeXTSTEP background) - the exact opposite of what you claim. Simplifying and summarizing is helpful, and I commend you for trying, but your answer contains fundamentally incorrect claims. – mklement0 Dec 12 '14 at 16:58
  • Let me try once again to address your points. I am cited `java` as an example command that came out of a shop that adhered closely to a BSDish philosophy. I could have dug through the odder commands on one of my BSD servers to find a more pedantically correct example, but java is (a) something that is on most *nix servers these days, and (b) probably the command I most frequently mess up the args by passing double dashes. As I mention above OSX is an amalgam of different UNIX heritages, with Apple's /Library, /System, etc overlay. _Eg_ `launchctl` is not BSD, _but is in that style_. – xcah Dec 12 '14 at 17:31
  • "Eg launchctl is not BSD, but is in that style." No, it is not, and that's exactly my point (please re-read my previous comment). You will _not_ find a bona fide BSD utility that uses something like `-version` (the only historical exception is `find`, which, however, calls such constructs _primaries_). The generalizations about options style in your answer are fundamentally flawed, and I think you're doing a disservice to others by not correcting them. – mklement0 Dec 12 '14 at 17:51