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., python_script.py). 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., run_pyscript.sh) 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 ‘./run_pyscript.sh 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 ‘python_script.py’.

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

vi python_script.py

This command opens the vi text editor.

nano python_script.py

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.
vi run_pyscript.sh

Here we create a bash script using vi named run_pyscript.sh

#!bin/bash

python3 python_script.py "$1" "$2"

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

Bash Script

python3 ‘calls the python3 interpreter. ‘python_script.py‘ 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 run_pyscript.sh

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. ‘run_pyscript.sh‘ 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:
./run_pyscript.sh 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

./run_pyscript.sh‘ 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.


References:

https://stackoverflow.com/questions/4377109/shell-script-execute-a-python-program-from-within-a-shell-script