Wordsplitting: How Your Shell Splits Command-Line Arguments

The user’s shell splits the command-line string into words before passing them as command-line arguments to Python, which assigns them to sys.argv.

../_images/argument_parser.png
  1. sys.argv[0] contains the program name
  2. sys.arg[1:] contains the list of command-line arguments

Note

The interpretation of command-line arguments, including word-splitting is dependent on the shell; not every shell parses the command-line string the same way. Generically speaking however, it splits words at whitespaces and trims any left over whitespace from the ends.

If it’s not in the list you’ll need to manually search for documentation specific to your shell.

Quoting & Escape Characters

Quoting can be used to:

  • Disable special treatment for special characters.
  • To prevent reserved words from being recognized as such.
  • To prevent parameter expansion.
  • To protect multi-word arguments from word spliting.

Escape characters can be used to:

  • Disable special treatment for a single special character.

Bash provides two types of quoting:

  1. Weak quoting: double-quotes
  2. Strong quoting: single-quotes

Windows only allows double-quotes

You don’t necessarily need to understand the details of each shell, but if you support command-utilities and a it’s helpful to know how the shells parse the command string before it ever gets to your Python program.