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
.
sys.argv[0]
contains the program namesys.arg[1:]
contains the list ofcommand-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.
Info on how shells/consoles parse command-line arguments:
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:
- Weak quoting:
double-quotes
- 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.
Learn More¶
References¶
[1] | http://mywiki.wooledge.org/Arguments_ |
[2] | https://docs.microsoft.com/en-us/cpp/c-languageparsing-c-command-line-arguments |
[3] | http://www.tcsh.org/tcsh.html/Lexical_structure.html |
[4] | https://www.dartmouth.edu/~rc/classes/ksh/arguments.html#top |
[5] | https://linux.die.net/man/1/zshexpn |
[6] | https://fishshell.com/docs/current/ |
[7] | http://wiki.bash-hackers.org/syntax/quoting |
[8] | https://ss64.com/bash/syntax-quoting.html |
[9] | https://ss64.com/nt/syntax-esc.html |