Python and Bash Integration in Linux: A Step-by-Step Guide

Linux Call Python Script From Bash With Argumentt

Bash scripts are powerful tools for automating tasks in the Unix/Linux environment. Combining the flexibility of Bash with the capabilities of Python allows you to create robust and efficient scripts. In this article, we will explore the process of calling Python script from bash with the example.

To call a Python script from Bash in Linux, first create the script (e.g., Use a text editor like vi or nano. The Python script should import sys for command-line arguments. Then, create a Bash script (e.g., with ‘#!/bin/bash’ and a line to execute the Python script with arguments. Make the Bash script executable using ‘chmod +x’ and run it with ‘./ arg1 arg2’.

Also read: How to Uninstall Python 3.7 from Ubuntu

How to Call a Python Script from Bash

  • First, you have to create a Python script. Here, we create a Python script named ‘’.

You can create a Python script by using any of the text editors—for example, nano, vi.


This command opens the vi text editor.


Alternatively, use this command for the nano text editor.

Here we are simply creating a Python script using the vi text editor.

Vi Command
  • Now we are creating a simple Python script. Press ‘i’ and now you are in the insert mode. Now you can start typing your Python code.
import sys
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])

result = num1 + num2 
print(f"The sum is: {result}")
Python Add

After entering your code, press ‘Esc’ to exit insert mode. To save and exit type ‘:wq!’ and press ‘Enter’. This writes the changes to the file and quits the editor.

import sys‘ this import sys module, which provides access to some variables used by the interpreter and functions that interact with the interpreter. ‘sys.argv‘, contains command line arguments passed to the script. ‘sys.argv[]‘, by using this the subsequent elements are the arguments provided when running the script. ‘float‘, converts the value into floating point number and assign it to the respective variable as num1 and num2. The last line simply prints the sum.

  • We are creating a Bash script that calls the Python script and passes arguments.

Here we create a bash script using vi named


python3 "$1" "$2"

We create a bash script named and pass the argument as python3 followed by the Python file name.

Bash Script

python3 ‘calls the python3 interpreter. ‘‘ is the name of the Python script to be executed. You can replace this with the actual name of your Python script. “$1” and “$2” are positional arguments or command-line arguments passed to the Bash script and represent the first and second arguments respectively.

Now press ‘Esc’ to exit insert mode. To save and exit type ‘:wq!’ and press ‘Enter’. This writes the changes to the file and quits the editor.

  • So before executing the Bash script, we have to make the Bash script executable. To make it executable you have to use the following command:
chmod +x

This command is used in the Unix/Linux operating system. ‘chmod‘ is used to change mode, it is used to change the permissions of a file. ‘+x‘, adds the execute permission to the file. ‘‘ is the name of the Bash script file for which we want to change the permission.

  • Now you can run the bash script. Execute the Bash script to call the Python script with arguments by using the following command:
./ arg1 arg2

Replace arg1 and arg2 with the desired values you want to pass as arguments to your Python script.
Here in this example, we pass values as 2 and 3.

Run Bash Command

./‘ executes the Bash script with values 2 and 3. Gives output as the sum of 2 and 3 which is 5. Similarly, you can pass arguments to a Python script from the command line.

Also read: macOS – How To Run Python Script On The Terminal?

Here we call a Python script from a bash script in Linux. First, we created a Python script using vi editor and then similarly created a bash script. After that, we make a bash script executable and then we run the bash script to call the python script with arguments. This technique is useful for integrating Python functionality into Bash scripts.