Call Python Script From Bash With Arguments

Python scripts can be executed in different ways. The common way includes using JuPyter Notebook, IDE, and even text editors. This article focuses on running the code on the command line, specifically the cases we have to pass arguments.

Command-line on Windows comes in the form of Power Shell, command prompt, or the new Windows terminal, whereas on Linux and macOS, we have the Terminal/ Bash.

This article discusses two ways of running Python scripts with arguments on bash.

  1. Using sys.argv() function
  2. Using Argparer

Note: Any command coming after $ (dollar sign) in this article must always be executed on the command line.

Method 1: Using sys.argv function

Importing the sys module into Python code allows us to pass command-line arguments through the function sys.argv. Let’s save the following two lines of code on

The general syntax for running Python files on the command line is:

python <options> <file_path> <args>

We won’t be passing any options in this article but focus on passing arguments, args. For the path, you can provide a full path or a relative path. If we assume that the script is on the Desktop, we can use the full path to run the file as follows:

$ python /home/Tomasz/Desktop/

Alternatively, we can use the command cd (change directory) to locate the folder containing the file and then run the script directly as follows:

$ cd /home/Tomasz/Desktop/

$ python



In this case, we did not pass any argument, and therefore sys.argv is a list containing the script’s name only. Let’s see some more examples with arguments:

$ python 12 4 -9 desk


['', '12', '4', '-9', 'desk']

Note that sys.argv will always have the first element, sys.argv[0], as the script’s name, followed by the arguments. The arguments passed are also always strings. Let’s modify the script (and name as and use it to add two integers:

Running the script on the command line:

$ python 12 4


['', '12', '4']

Printing sys.argv shows 3 elements as expected, but the arguments, 12 and 4, are of string types, and therefore we cast them fast into integers using the built-in int() function before adding them.

As a precaution on the number of arguments allowed, we issue an assertion statement to ensure that sys.argv list as two arguments only (2 arguments with the name of the script in index 0 makes it 3 elements).

Method 2: Using Argparse

Unlike sys, the argparse module allows us to have more control over the arguments being passed. Let’s start with a script named, which does not perform any computation but will enable us to see how arguments are passed.

Here are key points to note in the above code.

  1. ‘store_true’ and ‘store_false’ are used for storing the values True and False, respectively. If the default value is given, then this value takes precedence. For example, in the “–ignore-existing” argument default value is given as False, and therefore this overrides the action which stores True.
  2. The add_argument() must differentiate between optional and positional arguments. By default, arguments with a hyphen (1 or 2) are optional unless set as required, like in -B / –block-size. Any argument not starting with a hyphen is assumed positional and is required by default unless specified otherwise.
  3. The help value is a string containing a brief description of the argument. When a user requests help (usually by using -h or –help at the command line), these help descriptions will be displayed with each argument.
  4. The optional arguments (introduced by “-” or “–“or required parameter set to False) – a single hyphen will carry a single character “short option” (such as -a, -B, and -v above), and two hyphens are for multiple characters “long options” (such as –exclude and –ignore-existing above).

Let’s run the script on the command line.

$ python


usage: [-h] [-a] [-v] -B BLOCK_SIZE [--ignore-existing] [--exclude EXCLUDE] [--delete DELETE] src dest error: the following arguments are required: -B/--blocksize, src, dest


The execution leads to an error because the 3 required arguments have not been passed. No argument was given here.

$ python -B 256 “/home/Tomasz/Desktop/test1” “/home/Tomasz/Desktop/test2”


{'archive': False, 'verbose': False, 'blocksize': '256', 'ignoreexisting': False, 'exclude': None, 'delete': None, 'src': '/home/Tomasz/Desktop/test1', 'dest': '/home/Tomasz/Desktop/test2'}


There is no error this time around because we passed the required arguments. The optional arguments took their default values. The arguments are now on a Python dictionary and can be assessed using the keys (see more later).

$ python –help



We requested help using the –help argument and the help descriptions were printed. By default, ArgumentParser objects add an -h/–help option, displaying the parser’s help message. We also get to classify arguments as positional or optional.

Let’s create another script,, and use it to perform some simple computations.


$ python -a 10 -b 3.4 34.9


{'avalue': 10, 'bvalue': '3.4', 'c': 34.9}


We have issued default values for the -a and -b optional arguments. We have also given types for -a and c. By default, string values are parsed, and by using the type parameter, we can change this into any datatype we want. In this case, -a is set to int, and c is set to float.

The parser is not issued with a type parameter for -b, and therefore we have to cast it into a float explicitly as float(config[“bvalue”]).

The dictionary config contains all the arguments we passed, and we can use their keys to access respective values. The script computes a*b divided by c then the result is raised to power 3 to obtain 0.9246140111721999