Shell scripting: short or long format options?

Shell scripting: short or long format options?

This is something I get asked quite a lot, so I wanted to write a piece about it.

This is an extract from the manpage from GNU grep:

NAME
       grep, egrep, fgrep, rgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]

DESCRIPTION
       grep  searches  the  named  input  FILEs  for lines containing a match to the given PATTERN.  If no files are specified, or if the file “-” is given, grep searches standard input.  By
       default, grep prints the matching lines.

       In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively.  These variants are deprecated, but  are  provided  for  backward
       compatibility.

OPTIONS
   Generic Program Information
       --help Output a usage message and exit.

       -V, --version
              Output the version number of grep and exit.

   Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.

       -P, --perl-regexp
              Interpret the pattern as a Perl-compatible regular expression (PCRE).  This is highly experimental and grep -P may warn of unimplemented features.

So, -F is a short format form, while --fixed-strings is a long format form.

When should I use either form?

My take is: when working interactively, do whatever you like. Whenever you are writing a script to be reused, strive to use the long format options as much as possible.

Why?

Nobody cares about what you do on your computer. But whenever you write a script that should, later on, be used and read by other people - or just yourself in the future - the long form is often self-explanatory. Instead of opening the manpage and looking for cryptic -K -X -i, you can just read the option! It will for sure save more time than what you spend while typing a few more chars.

There're a few exceptions to this rule:

  • if the option is very common. Feel free to use -E and -P for grep, as they're very well known and widespread options for this command.
  • if you need portability between different OSes (e.g. MacOS+Linux+BSD) and the long format option is not available somewhere; this sometimes happens.

Why is this cool?

Consider this command, which is the recommended way to install the great MacOS package manager Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Such curl command is quite common in many installers; can you tell what such options do? Can't you? Consider this line:

/usr/bin/ruby -e "$(curl --fail --silent --show-error --location https://raw.githubusercontent.com/Homebrew/install/master/install)"

I bet you can now understand what it does; maybe the last flag is a bit vague (it tells curl to follow redirects via Location headers), but the overall feeling is simply... great!

Photo by Alex Holyoake on Unsplash