Shell – Common flag designations and standards for shell scripts and functions

gnuposixshellshell-script

I have been adjusting to using GETOPT and GETOPTS in my shell scripts and custom functions to enable me to use the functions that I create more flexibly. After using a number of UNIX functions I have realized that a lot of them use a somewhat similar syntax.

Examples being -v for verbose output and -R/-r for recursive functionality.

I looked at the POSIX standard guide for utility conventions and the GNU's option tables for C based utilities. In an attempt to understand whether this was by design and if there are any rules that I should be following.

My questions are:
1. Are there are any specific option flags that have designated meaning?
2. How much freedom do I have with my options and which rules shouldn't I break?
3. Should I stay way from giving options like -t and -T different meanings, or should I group them together for simplicity?
4. What situations should I use uppercase or lowercase letters?

Best Answer

There are no fast and firm rules, or even common conventions. At most, there are a few options that are used consistently across some common utilities — but not across all common utilities.

Here are a few common letters — but remember that these are by no means universal conventions. If you have one of the features described below, it's better if you use the corresponding option. If one of the options doesn't make sense for your utility, feel free to use it for something else.

  • -c COMMAND or -e COMMAND: execute a command. Examples: sh -c, perl -e.
  • -d or -D: debug.
  • -f: force, don't ask for confirmation for dangerous actions.
  • -h: help — but many utilities only recognize the long option --help or nothing at all. Examples: Linux getfacl, mount. Counter-examples: GNU ls, du, df (no short option, -h is human size), less (-? is help, -h is something else).
  • -i: prompt for confirmation (interactive).
  • -n: do not act, just print what would be done. Example: make.
  • -r or -R: recursive.
  • -q or -s: quiet or silent. Example: grep -q means display no output, grep -s means display no error message.
  • -v: verbose.
  • -V: show version information.

Traditionally lowercase letters are used, and uppercase letters only came into use because there are only 26 lowercase letters. Sometimes uppercase letters have something to do with the corresponding lowercase letter (example: GNU grep -h/-H, ssh -x/-X, cp -r/-R), sometimes not.

Related Question