Python is a popular programming language used in a wide range of applications, from data analysis and scientific computing to web development and artificial intelligence. Bash is a powerful scripting language used primarily in Unix and Linux environments for automating tasks and running commands. By combining the two languages, developers can create powerful scripts that can automate complex tasks and workflows. In this article, we’ll explore various techniques for running Python scripts with bash, including passing arguments, activating virtual environments, and running scripts in the background. We’ll also provide examples of real-world scripts that demonstrate these techniques in action.
In this tutorial you will learn:
- How to run Python scripts from a bash script
- Passing arguments to Python scripts from a bash script
- Activating virtual environments for Python scripts in a bash script
- Running Python scripts in the background using a bash script

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
How to run Python scripts from a bash script
To run a Python script from a bash script, we should first change to the directory containing the Python script using the cd
command, and then use the python
command to execute the script. Here’s an updated example:
#!/bin/bash
# Change to the directory containing the Python script
cd /path/to/python/script/directory
# Run the Python script
python myscript.py
In this updated script, we first change to the directory containing the Python script using the cd
command, and then run the python
command to execute the myscript.py
Python script. Note that the path to the script file should be specified relative to the current directory or as an absolute path.
Passing arguments to Python scripts from a bash script
We can pass arguments to a Python script from a bash script by specifying them after the Python script file name when using the python
command. The arguments will be accessible within the Python script through the sys.argv
list. Here’s an example:
#!/bin/bash
cd /path/to/python/script/directory
python myscript.py $1 $2 $3
In this script, we run the myscript.py
Python script and pass three arguments ($1
, $2
, and $3
) to it.
Within the Python script, we can access the arguments using the sys.argv
list:
import sys
print("Argument 1:", sys.argv[1])
print("Argument 2:", sys.argv[2])
print("Argument 3:", sys.argv[3])
In this example Python script, we access the three arguments passed from the bash script using the sys.argv
list and print them to the console. By passing arguments from a bash script to a Python script, we can customize the behavior of the script and make it more flexible and reusable.
Activating virtual environments for Python scripts in a bash script
When working with Python, it’s common to use virtual environments to isolate project dependencies and ensure consistent runtime environments. If you’re using a virtual environment for your Python script, you’ll need to activate it before running the script. Here’s an example of how to activate a virtual environment for a Python script within a bash script:
#!/bin/bash
# Activate the virtual environment
source /path/to/venv/bin/activate
# Change to the directory containing the Python script
cd /path/to/python/script/directory
# Run the Python script
python myscript.py
In this script, we use the source
command to activate the virtual environment located at /path/to/venv
. We then change to the directory containing the Python script using the cd
command and run the script using the python
command.
Note that the path to the virtual environment may vary depending on your setup, so be sure to modify the path to match your own environment. Additionally, if your virtual environment requires specific dependencies that aren’t installed on the system by default, you may need to install them first within the virtual environment before running the script.
Running Python scripts in the background using a bash script
Running a Python script in the background can be useful when you want to run the script without keeping the terminal window open. In a bash script, you can use the nohup
command to run a Python script in the background, and redirect its output to /dev/null
to prevent it from filling up the disk space. Here’s an example:
#!/bin/bash
# Change to the directory containing the Python script
cd /path/to/python/script/directory
# Run the Python script in the background
nohup python myscript.py > /dev/null 2>&1 &
In this script, we first change to the directory containing the Python script using the cd
command. We then use the nohup
command to run the Python script in the background, followed by the python
command and the name of the script file.
We redirect the output of the script to /dev/null
, which is a special file that discards any data written to it. This prevents the script’s output from filling up the disk space or interfering with other processes. Finally, we append the &
symbol to run the script in the background.
When you run this script, the Python script will start running in the background and you’ll see a message similar to this:
nohup: ignoring input and appending output to ‘nohup.out’
You can now close the terminal window and the Python script will continue running in the background until it finishes or is terminated manually.
Running Python scripts in the background can be useful for long-running scripts, such as web servers or data processing tasks, that don’t require user input or interaction. By using a bash script to run the Python script in the background, you can automate the process and ensure that it continues running even after you log out of the terminal.
Conclusion
In this article, we explored various techniques for running Python scripts with bash, including passing arguments, activating virtual environments, and running scripts in the background. These techniques can greatly enhance the power and flexibility of Python scripts, enabling developers to automate complex tasks and workflows with ease. By mastering the art of running Python scripts with bash, developers can unlock the full potential of these powerful scripting languages and take their skills to the next level. With the foundational knowledge and skills provided in this article, developers can create robust and efficient scripts that can handle a wide range of use cases and unlock new possibilities for automation and efficiency.