Introduction
In Linux automation, Bash is excellent for gluing together command-line tools. However, as logic becomes more complex, Python often becomes a better choice because it offers clearer structure, better data handling, and a rich standard library.
This is why many operations teams use both: Bash for short shell-native tasks and Python for more maintainable automation scripts.
In this lab, you will take your first steps in using Python within a Linux environment. You will learn how to check your Python version, use the interactive shell for quick tests, and write a Python script that behaves just like a native Linux command.
Check Python Version
Before writing any code, it is crucial to know which version of the Python interpreter is installed on your system. Most modern Linux distributions, including the Ubuntu system you are using, come with Python 3 pre-installed.
For operations work, this version check is also about compatibility. Different Python versions can add or remove language features and library behavior. A script that works on one machine can fail on another if you silently assume the interpreter is identical everywhere.
In this step, you will verify the installed Python 3 version. This ensures that the environment is ready for the scripts you will write later.
Open your terminal and run the following command to check the version:
python3 --version
You should see output similar to this:
Python 3.10.12
The exact version number might vary slightly, but as long as it is Python 3.x, you are good to go. Note that we use python3 explicitly because python (without the 3) might refer to the older, deprecated Python 2 on some legacy systems, or it might not be linked at all.
Use the REPL
Python provides an interactive shell, often called the REPL (Read-Eval-Print Loop). This is an incredibly powerful tool for testing snippets of code, checking library availability, or performing quick calculations without the need to create a file.
The REPL is useful because it shortens the feedback loop. Instead of editing a file, saving it, and running it repeatedly, you can test one expression at a time. Many engineers use the REPL the same way they use a shell prompt: as a place to experiment before turning an idea into a real script.
In this step, you will enter the Python interactive shell, run a simple command, and then exit back to the Bash shell.
First, start the Python 3 interactive shell by typing:
python3
Your command prompt will change to >>>, indicating that you are now inside the Python environment.
Now, type a simple Python command to print a message:
print("Hello from Python REPL")
You will see the output immediately.
To exit the Python shell and return to your standard Linux Bash shell, type:
exit()
Alternatively, you can press Ctrl + D.
After exiting, you should see your standard command prompt (e.g., labex:~/project $) again.
Create a Script
While the REPL is great for testing, real automation requires saving code into scripts. A Python script is simply a text file containing Python code.
The practical advantage of a script over a one-off command is repeatability. Once the logic is in a file, you can review it, version it with Git, run it again tomorrow, and hand it to another engineer without retyping anything.
In this step, you will create a file named system_info.py.
Use the nano text editor to create and open the file:
nano system_info.py
Add the following content to the file:
print("Starting system info script...")
To save and exit nano:
To save your changes in the nano editor, press Ctrl + O and then hit Enter to confirm the filename. Finally, press Ctrl + X to exit the editor and return to your terminal.
Now, verify that the file has been created:
ls -l system_info.py
Use Standard Library
One of Python's greatest strengths is its "batteries-included" standard library. Unlike Bash, where you often have to parse the output of commands like uname or cat /etc/os-release, Python provides built-in modules to access system information cleanly.
This is a major reason Python is common in operations tooling. Instead of stitching together many small text-processing commands, you can work with structured functions and objects directly. That usually makes scripts easier to read and less fragile when output formats change.
In this step, you will modify your script to use the platform library to print details about the operating system.
Open your script again with nano:
nano system_info.py
Modify the content to look like this:
import platform
print("Starting system info script...")
print(f"System: {platform.system()}")
print(f"Release: {platform.release()}")
print(f"Version: {platform.version()}")
Save and exit nano (Ctrl+O, Enter, Ctrl+X).
You can test your script by passing it explicitly to the python interpreter:
python3 system_info.py
You should see output detailing your Linux environment.
Execute the Script
In the previous steps, you ran the script using python3 system_info.py.
Calling the interpreter explicitly is the clearest beginner-friendly pattern because it removes ambiguity. The shell does not need to guess how to run the file, and you can see exactly which interpreter is executing your code.
In standard Linux usage, you might sometimes see scripts executed directly like ./system_info.py. This requires two things:
- The "shebang" line (which we skipped).
- "Execute" permissions on the file.
Since we are working in a streamlined environment without shebangs, we will stick to the most reliable method: running the script explicitly with the Python interpreter.
Try running your script one more time to confirm it still works:
python3 system_info.py
This explicit command tells Linux exactly what to do: "Use the program python3 to read and execute the instructions in the file system_info.py."
Summary
In this lab, you have successfully bridged the gap between Bash and Python. You learned how to:
- Check the Python version to ensure environment compatibility.
- Use the REPL for interactive testing.
- Create a Python script.
- Use the standard
platformlibrary to access system data. - Run the script using the
python3command.
These skills form the foundation of Python scripting in Linux, allowing you to write more complex and readable automation tools than is typically possible with Bash alone.



