Would you like to learn how to execute Shell commands using Python? In this tutorial, we are going to show you how to use Python to execute Shell commands on a computer running Linux.

• Ubuntu 20
• Ubuntu 19
• Ubuntu 18
• Python 3.8.5

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Python – Executing Shell commands

Use Python to execute a system command.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

In our example, Python will display the command output directly on the screen.

In our example, Python will not continue until the system command is finished.

Tutorial Python – Executing system commands

Use Python to execute a system command.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

In our example, Python will store the command result in a variable.

In our example, Python will not continue until the system command is finished.

Tutorial Python – Execute a system command as sub-process

Execute a system command as a sub-process.

Copy to Clipboard

Optionally, split the command as an array.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

In our example, Python will spawn a new process to execute the system command.

Tutorial Python – Execute a Shell command as sub-process

Execute a system command as a sub-process.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

In our example, Python will spawn a new process to execute the system command.

In our example, Python will store the command result in a variable.

Congratulations! You are able to use Python to run Shell commands.