Call Python Script from Bash with Arguments

Call Python script from bash with arguments

Python is a high-level language famous for its simplicity, flexibility, and readability. At the same time, Bash is a Unix shell and command language used primarily on Unix and Linux systems. Data analysis, machine learning, web development, and scientific computing tasks are widely performed using python language. On the contrary, the bash is used for the tasks like file manipulation, process management, and system administration.

The python script can be written using IDEs like PyCharm, Spyder, Jupyter Notebook, Visual Studio Code, Sublime Text and IDLE, as the bash is a command language, so the commands are given to the terminal or command prompt.

Though Python and Bash are used for different purposes, python and bash are used together for tasks like running system commands, task automation, and other tasks. The python script can be written in any IDE, and then by using the command line, the bash can call the python script as it becomes difficult for bash to perform tasks like data analysis, file manipulation etc.

For every solution, we need to write a python script and Call the python script through bash. So let’s learn it for each approach below.

Using sys.argv

Create a python file and write a script where two arguments are taken and displayed on the output screen.

The import sys imported the sys module to get the command-line arguments passed to the script. After that, we assigned the first argument to arg1 and the second to arg2. Then, finally, we printed the values of arg1 and arg2 to the console.

The above script will be saved with the name my_code.py (as bash will require to call the script). The values of arg1 and arg2 will be given to the script through the bash command in the next step.

Call the python script my_code.py created in the previous code example. You can use the following command to call a Python script from Bash with arguments.

The shebang #!/bin/bash indicates this is a Bash script. Then, the python command called our Python script my_code.py. Finally, we passed the arguments hello and world.

Using argparse

Create a python file and write a script where two string arguments are taken and displayed on the output screen.

First, import the argparse module and create an argument parser using argparse.ArgumentParser(). We then added two required arguments to the parser using add_argument(). Each argument has a name (arg1, arg2), a type (str), and a help string that describes the argument. After that, we parsed the command-line arguments using parser.parse_args(), which returns an args object containing the values of the arguments.

The above script will be saved with the name my_code.py (as bash will require to call the script). The values of arg1 and arg2 will be given to the script through the bash command in the next step.

Call the python script my_code.py created in the previous code fence. You can use the following command to call a Python script from Bash with arguments.

The shebang #!/bin/bash indicates this is a Bash script. Then, the python command called our Python script my_code.py. Finally, we passed the arguments hello and world along with -a and -b. The string parameters were passed as the type of arguments were set to str. The code generates the error if a user tries to enter a number.

Use python or python3 to run the my_code.py as per the installed python version. For example, to check the version of python, type python --version.

In the above examples, two arguments are passed to the python script. You can add more parameters or as allowed by the python script.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *