Bash vs. Python: Choosing the Right Tool for Automation
Written on
Chapter 1: Introduction to Shell Scripting
Computers function as digital devices that comprehend specific binary instructions. Without an operating system, users can only access built-in firmware, like BIOS utilities. Operating systems enhance usability by providing a platform to run pre-existing software applications, such as word processors and web browsers. Most contemporary operating systems offer users both a Graphical User Interface (GUI) and a Command Line Interface (CLI).
Programmers often prefer the CLI because it aligns more closely with everyday programming tasks compared to GUIs. They frequently deploy software, manage files, and configure systems through CLIs. While the CLI is efficient for executing commands, we often find ourselves repeating similar commands with slight variations.
This repetition led to the emergence of shell scripting, allowing users to run sequences of pre-defined commands from files. Bash is a widely recognized command language integrated into most operating systems for executing shell scripts. Conversely, many developers opt for Python as an alternative for shell scripting due to its built-in features that Bash lacks.
In this discussion, I will compare Bash and Python from a shell scripting viewpoint and introduce some lesser-known techniques to enhance your automation skills using both languages.
Chapter 2: Bash: The Traditional Shell Scripting Language
The essence of shell scripting lies in executing pre-defined command sequences using a shell interpreter. Bash interprets each input as a command, facilitating automation. Recall your first experience with the Bash terminal—perhaps during your college days—when you navigated without extensive documentation.
Bash is not a general-purpose programming language; it encourages the use of external programs. For instance, you might use expr 10 + 15 to perform arithmetic. However, modern Bash offers built-in features that cater to general programming needs, reducing the necessity to call external programs in many cases.
For example, you can perform basic arithmetic using arithmetic expansions directly in the terminal:
Bash executes commands natively without requiring extensive syntax while providing minimal syntax for general programming needs.
Chapter 3: Python: A Versatile Alternative
If Bash can execute commands and supports various programming features, why do many developers prefer Python for automation? Early Python documentation reveals that it was designed to merge shell scripting capabilities with operating system-level programming.
Bash cannot directly access operating system-level APIs (C APIs). When automation scripts require C API access, developers often resort to creating executables with other programming languages. Python addresses this limitation by providing a minimal, automation-friendly language with direct access to C APIs, allowing for cross-platform functionality.
Python approaches programming from a general-purpose perspective, which means it does not natively execute external programs but offers a user-friendly child process API.
Now that we've explored the fundamental goals of both languages, let's delve into a comparison of their automation capabilities.
Chapter 4: Bash vs. Python: Choosing the Right Tool
Programmers create various shell scripts, some of which execute numerous POSIX commands (e.g., mv, cp). In different scenarios, they may need to incorporate data processing or operating system-level operations into their scripts, and sometimes they require cross-platform compatibility.
As is common in programming comparisons, there is no definitive winner; the ideal choice for shell scripting depends on the specific development scenario.
Bash is advantageous for:
- Automating POSIX command-line tasks with minimal data processing, such as system administration scripts.
- Writing shell scripts that handle configurations or miscellaneous tasks via other CLI applications, like Git commit messages or application deployment using CLI tools.
- Portability on Unix systems, as the Bash interpreter is typically pre-installed more widely than Python.
Python excels in:
- Automating tasks that involve extensive data processing or low-level API access, rather than merely executing CLI commands.
- Creating cross-platform automation scripts that perform commands, utilize low-level APIs, and handle data processing across various operating systems like GNU/Linux, Windows, and macOS.
In summary, Bash provides a straightforward, native, terminal-like approach for automation scripts, while Python serves as a versatile, cross-platform language for tasks requiring low-level API access and complex data handling.
Chapter 5: Integrating Bash and Python
There is no intense rivalry between Bash and Python, as they represent different programming paradigms—Bash is a command language, whereas Python is a general-purpose language. Depending on your needs, you can choose one or utilize both.
For instance, consider this Bash script to add two decimal numbers:
#!/bin/bash
sum=$(bc <<< "1.5 + 2.51")
echo $sum
You can achieve the same result in Python:
#!/bin/bash
sum=$(python3 <<< "print(1.5 + 2.51)")
echo $sum
Many programmers integrate Bash within their Python scripts for convenience rather than relying on multiple third-party Python libraries. For example, the following Python script retrieves the process identifier for the Gedit program:
#!/usr/bin/env python3
import subprocess
gedit_pid = subprocess
.getoutput("ps -ef | grep gedit | head -1 | awk '{print $2}'")
.strip()
print(gedit_pid)
Chapter 6: Addressing Limitations in Bash and Python
As previously mentioned, both Bash and Python have limitations when it comes to modern automation needs. Crafting shell scripts that require operating system-level API access or intricate data manipulation can be challenging with Bash. Conversely, executing command-line operations in Python using the subprocess API can feel less intuitive.
If you require C API access within Bash, consider utilizing the ctypes.sh extension. There is even an HTTP web server named httpd.sh built using the ctypes.sh foreign function interface (FFI).
The pysh project provides an easy method to execute Bash commands in Python scripts using the > character, as shown in the following example:
for i in xrange(100):
index = "%02d" % i
> mv from$index.txt to$index.txt
The zxpy project, inspired by Google's zx, enables productive execution of command-line operations in Python:
#! /usr/bin/env zxpy
~'echo Hello world!'
file_count = ~'ls -1 | wc -l'
print("file count is:", file_count)
Chapter 7: Conclusion
The concept of shell scripting originated with the Thompson shell in the 1970s, aimed at executing command-line operations from files for automation purposes. The modern DevOps movement has expanded this concept to include RESTful API calls, data processing, and various DevOps-related tasks in automation scripts.
Using Bash for shell scripting remains relevant for modern automation since it allows for native command execution without a separate child process API. However, when tasks extend beyond native command operations, Python is frequently adopted as a contemporary alternative for automation.
Learn more about modern Bash scripting techniques in the following story:
Check out some unique Python use cases in this next video:
Thank you for reading!
Level Up Coding
We appreciate you being part of our community! Before you leave: 👏 Clap for this article and follow the author 👉📰 Explore more content in the Level Up Coding publication 💰 Free coding interview course ⇒ View Course 🔔 Follow us: Twitter | LinkedIn | Newsletter 🚀👉 Join the Level Up talent collective and find an amazing job.