Pythonic arguments

We should know by now that we will want to pass command-line arguments to Python and we can do this using the argv array. However, we are more like bash; with Python we combine the program name into the array with the other arguments.

Python also uses lowercase instead of uppercase in the object name:

  • The argv array is a part of the sys object
  • sys.argv[0] is the script name
  • sys.argv[1] is the first argument supplied to the script
  • sys.argv[2] is the second supplied argument and so on
  • The argument count will always be at least 1, so, keep this in mind when checking for supplied arguments

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.