Bash – Correct Way to Type Command Parameters & Arguments

argumentsbashparameter

Which is the correct way – to separate the argument from the parameter with a whitespace, or no?

And should you separate arguments or stick them all together?

Examples:

<command> -D 192.168.0.100 -p 80
<command> -cvvfz

or:

<command> -D192.168.0.100 -p80
<command> -c -vv -f -z

Do some programs accept only one "style", or does it matter at all, generally speaking?

Best Answer

This blog can help you out.

A small fragment of this blog is here:

Flag Argument

Many flags accept an option called a "flag argument" (not to be confused with a "command argument"). In general a command's parameters can be in any order, but flags that accept options must have the option directly after the flag. That way, the command doesn’t get confused by non-flag arguments.

For an example, here the -x flag does not accept an option but the -f flag does. archive.tar is the option being passed to -f. Both of these are valid.

tar -x -f archive.tar
tar -xf archive.tar

A flag and its option can be separated by a space or an equals sign =. Interestingly, short flags (but not long flags) can even skip the space, although many people find it much easier to read with the space or equals sign. These three are all valid and equivalent.

tar -f archive.tar
tar -f=archive.tar
tar -farchive.tar

Long flags must have a space or equals sign to separate the flag from its option.

git log --pretty=oneline git log --pretty oneline

Other resources:

  1. https://softwareengineering.stackexchange.com/a/307472
Related Question