DEV Community

Cover image for Bash vs Python Scripting: A Simple Practical Guide
Hussein Alamutu
Hussein Alamutu

Posted on • Edited on

Bash vs Python Scripting: A Simple Practical Guide

Bash and Python are two popular scripting languages used for automation and system administration tasks.

This article aims to provide a simple, practical guide for beginners to understand the differences between Bash and Python scripting and when to use each one.

By the end of this article, readers will have a better understanding of the basics of Bash and Python scripting, as well as their strengths and weaknesses in different scenarios.

Prerequisites

To follow this tutorial you need the following:

  • Access to a linux machine(Ubuntu, Cent OS etc.) to enable you run bash commands on the terminal, since Bash is unix-based. If you don’t have that, git bash or Windows Subsystem for Linux(wsl) will do.
  • Python installation, whether Linux, Windows or MacOS, most times python comes pre-installed, you can confirm that by running python —-version on your system command line interface, if python exists, you will get the version number of the python on your system. If not you can install it by following this Python guide.

With that been said, let’s dive into the guide properly, fasten your seatbelts.

Bash script been open on hussein alamutu’s ubuntu command line

Bash Scripting

What is Bash & Bash Scripting?

Bash is a command-line shell used on Linux, macOS, and other Unix-like operating systems. While, Bash scripting is commonly used for automation, system administration, and software deployment tasks.

Bash scripts are easy to write and execute, and they can perform complex operations using a few lines of code. Bash provides many built-in commands, such as "echo"(used to print), "cd"(to change directory), "ls"(list), and "grep"(searches for a match to a given pattern), that can be used in scripts.

Bash scripts can be used to manipulate files, perform backups, and configure system settings, among other tasks.

Basics of Bash Scripting

Here's an explanation of the basics of Bash scripting:

1.Variables:

  • In Bash, a variable is used to store a value, such as a number or a string.

  • Variables can be declared and assigned a value using the equals sign (=).

  • For example, x=10 assigns the value 10 to the variable x.

2.Conditionals:

  • In Bash, conditionals are used to make decisions based on a certain condition.

  • The if statement is used to check whether a condition is true, and the else statement is used to specify what to do if the condition is false.

  • For example:



x=10
if [ $x -gt 5 ]
then
    echo "x is greater than 5"
else
    echo "x is less than or equal to 5"
fi


Enter fullscreen mode Exit fullscreen mode

3.Loops:

  • In Bash, loops are used to iterate over a sequence of values or to repeat a block of code a certain number of times.

  • The for loop is used to iterate over a sequence of values, while the while loop is used to repeat a block of code as long as a certain condition is true.

  • For example:



# Using a for loop to iterate over a list of values
fruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"
do
    echo "$fruit"
done

# Using a while loop to repeat a block of code
x=0
while [ $x -lt 10 ]
do
    echo "$x"
    x=$((x+1))
done


Enter fullscreen mode Exit fullscreen mode

4.Functions:

  • In Bash, functions are used to encapsulate a block of code that can be called repeatedly with different inputs.

  • Functions are defined using the function keyword, and they can have inputs and outputs.

  • For example:



# Defining a function to calculate the area of a rectangle
function calculate_rectangle_area {
    width=$1
    height=$2
    area=$((width * height))
    echo $area
}

# Calling the function with different inputs
echo $(calculate_rectangle_area 3 4) # Output: 12
echo $(calculate_rectangle_area 5 6) # Output: 30


Enter fullscreen mode Exit fullscreen mode

These are the basic building blocks of Bash scripting, and they can be combined to create more complex scripts.

More Bash Scripts

Here are some examples of Bash scripts for common tasks:

1. File manipulation:

  • A script that renames all files in a directory with a specific extension to have a new prefix.

  • A script that searches for a particular string in a file and replaces it with a new value.

  • A script that compresses all files in a directory into a single archive file.

Check this github repo for sample scripts of the above bash scripting.

2. System administration:

  • A script that backs up all files in a directory to a remote server using secure copy (SCP).

  • A script that monitors system logs for a particular error and sends an email alert when it occurs.

  • A script that automates the installation of software packages and updates on multiple servers.

For the system administration part, here's an example of a Bash script that automates the installation of software packages and updates on multiple servers:



#!/bin/bash

# List of servers to update
servers=("server1" "server2" "server3")

# Software packages to install
packages=("apache2" "mysql-server" "php")

# Update package lists on all servers
for server in "${SERVERS[@]}"; do
  ssh $server "sudo apt-get update"
done

# Install packages on all servers
for server in "${SERVERS[@]}"; do
  ssh $server "sudo apt-get install ${PACKAGES[@]} -y"
done


Enter fullscreen mode Exit fullscreen mode

In this script, we first define a list of servers to update and a list of packages to install or update. We then loop through each server in the list and run the "apt-get update" command to update the system packages. We then loop through each package in the list and install or update it using the "apt-get install" command.

The -y option is used with apt-get install to automatically answer "yes" to any prompts during the installation process.

Note that this script assumes that you have SSH access to the servers and that you have sudo privileges to install software packages.

Coding with a python book on the table

Python Scripting

What is Python & Python Scripting?

Python is a general-purpose programming language used for a wide range of applications, including web development, data analysis, AI and machine learning. Python provides a clear, concise syntax that is easy to read and write
Python has a large standard library and many third-party modules that can be used to perform complex operations

Python scripting is commonly used for automation, data processing, and scientific computing tasks. Python scripts can be used to scrape data from websites, process large datasets, and automate repetitive tasks, among other things.

Basics of Python Scripting

Here's an explanation of the basics of Python scripting:

1.Variables:

  • In Python, a variable is used to store a value, such as a number or a string.

  • Variables can be declared and assigned a value using the equals sign (=).

  • For example, x = 10 assigns the value 10 to the variable x.

2.Conditionals:

  • In Python, conditionals are used to make decisions based on a certain condition.

  • The if statement is used to check whether a condition is true, and the else statement is used to specify what to do if the condition is false.

  • For example:



x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")


Enter fullscreen mode Exit fullscreen mode

3.Loops:

  • In Python, loops are used to iterate over a sequence of values, such as a list or a string.

  • The for loop is used to iterate over a sequence of values, while the while loop is used to repeat a block of code as long as a certain condition is true.

  • For example:



# Using a for loop to iterate over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# Using a while loop to repeat a block of code
x = 0
while x < 10:
    print(x)
    x += 1


Enter fullscreen mode Exit fullscreen mode

4.Functions:

  • In Python, functions are used to encapsulate a block of code that can be called repeatedly with different inputs.

  • Functions are defined using the def keyword, and they can have inputs and outputs.

  • For example:



# Defining a function to calculate the area of a rectangle
def calculate_rectangle_area(width, height):
    area = width * height
    return area

# Calling the function with different inputs
print(calculate_rectangle_area(3, 4)) # Output: 12
print(calculate_rectangle_area(5, 6)) # Output: 30


Enter fullscreen mode Exit fullscreen mode

These are the basic building blocks of Python scripting, and they can be combined to create more complex programs.

Python Modules & How to Use Them in Scripts

Python modules are pre-written pieces of code that can be imported into a script to add functionality such as working with files, processing data, sending emails, and more. . Here are some common Python modules and how to use them in scripts:

1.os module:

  • This module provides a way to interact with the underlying operating system.

  • Functions like os.chdir() to change the current working directory, os.mkdir() to create a new directory, and os.path.exists() to check if a file or directory exists can be used.

  • Example:



import os

# Change the current working directory
os.chdir('/path/to/new/directory')

# Create a new directory
os.mkdir('new_directory')

# Check if a file exists
if os.path.exists('/path/to/file'):
    print('File exists')
else:
    print('File does not exist')


Enter fullscreen mode Exit fullscreen mode

2.datetime module:

-This module provides a way to work with dates and times.

  • Functions like datetime.datetime.now() to get the current date and time, datetime.timedelta() to calculate the difference between two dates or times, and datetime.datetime.strptime() to convert a string to a date or time object can be used.

  • Example:



import datetime

# Get the current date and time
current_time = datetime.datetime.now()
print(current_time)

# Calculate the difference between two dates or times
time_diff = datetime.timedelta(days=1)
yesterday = current_time - time_diff
print(yesterday)

# Convert a string to a date or time object
date_string = '2023-03-20'
date_object = datetime.datetime.strptime(date_string, '%Y-%m-%d')
print(date_object)


Enter fullscreen mode Exit fullscreen mode

3.csv module:

  • This module provides a way to read and write comma-seperated-value(CSV) files.

  • Functions like csv.reader() to read a CSV file, and csv.writer() to write to a CSV file, can be used.

  • Example:



import csv

# Read a CSV file
with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

# Write to a CSV file
with open('output.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['Name', 'Age', 'City'])
    writer.writerow(['Alice', '25', 'New York'])
    writer.writerow(['Bob', '30', 'San Francisco'])



Enter fullscreen mode Exit fullscreen mode

4.shutil module:

  • The shutil module provides a higher-level interface for working with files and directories than the os module. It provides functions for copying, moving, and deleting files and directories.

  • For example:



import shutil

# Copy a file from one directory to another
shutil.copy("source/file.txt", "destination/file.txt")

# Move a file from one directory to another
shutil.move("source/file.txt", "destination/file.txt")

# Delete a file
os.remove("file.txt")


Enter fullscreen mode Exit fullscreen mode

5.requests module:

  • The requests module provides a way to send HTTP requests and handle responses.

  • You can use it to download files, interact with web APIs, and scrape web pages.

  • Example usage:



import requests

# Download a file
url = "https://example.com/file.txt"
response = requests.get(url)
with open("file.txt", "wb") as file:
    file.write(response.content)

# Get data from a web API
url = "https://api.example.com/data"
response = requests.get(url, headers={"Authorization": "Bearer YOUR_TOKEN"})
data = response.json()


Enter fullscreen mode Exit fullscreen mode

These are just a few examples of common Python modules and their usage. There are many other modules available that can help you accomplish various tasks in your scripts. You can search for them in the Python Package Index (PyPI) or through the Python documentation.

More Python Scripts

1. Web Scraping

  • Python is a popular language for web scraping, as it provides easy-to-use libraries like BeautifulSoup and requests.

  • Here's an example script that scrapes the top headlines from the BBC News website:



import requests
from bs4 import BeautifulSoup

url = "https://www.bbc.com/news"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

headlines = soup.find_all("h3", class_="gs-c-promo-heading__title")
for headline in headlines:
    print(headline.text)


Enter fullscreen mode Exit fullscreen mode

This script uses the requests library to send an HTTP GET request to the BBC News website and fetch the HTML content. It then uses the BeautifulSoup library to parse the HTML and extract the headlines using a CSS selector.

2. Data Analysis

  • Python is a popular language for data analysis, with many powerful libraries such as pandas, numpy, and matplotlib available for use.

  • Here's an example Python script for data analysis using the pandas library:



import pandas as pd
import matplotlib.pyplot as plt

# Load data from a CSV file
data = pd.read_csv('data.csv')

# Print the first five rows of the data
print(data.head())

# Compute descriptive statistics
print(data.describe())

# Compute the correlation matrix
corr_matrix = data.corr()
print(corr_matrix)

# Plot a histogram of one of the variables
data['variable_name'].hist()

# Plot a scatter plot of two variables
data.plot.scatter(x='variable1', y='variable2')

# Group data by a categorical variable and compute summary statistics
grouped_data = data.groupby('category')['variable'].agg(['mean', 'std', 'count'])
print(grouped_data)

# Plot a bar chart of the means for each category
grouped_data['mean'].plot(kind='bar')

# Save a plot to a file
plt.savefig('output.png')


Enter fullscreen mode Exit fullscreen mode

This script loads data from a CSV file, computes descriptive statistics and a correlation matrix, plots a histogram and a scatter plot of two variables, groups data by a categorical variable and computes summary statistics, plots a bar chart of the means for each category, and saves a plot to a file.

You can modify this script to analyse your own data by replacing the file name, variable names, and categorical variable with the appropriate names for your data.

Bash vs Python: A Fair Comparison

This section covers various aspects that should be considered while comparing Bash and Python scripting languages.

A. Syntax and readability:

The syntax of Bash scripting language is more complex and harder to read and understand than Python. Bash uses various special characters and symbols to represent different actions, which can make it harder to read and maintain. In contrast, Python's syntax is more straightforward, using indentation to denote code blocks, and a clean, easy-to-read syntax that is more accessible to beginners and experts alike.

B. Functionality and capabilities:

Python provides a wide range of libraries and modules that allow for a wide range of functionalities, such as data analysis, web development, artificial intelligence, and more. On the other hand, Bash scripting is mostly used for automating system-level tasks and commands, such as file management and system administration.

C. Performance and execution time:

Bash scripts tend to run faster than Python scripts because they do not require an interpreter to execute the code. Bash scripts are directly executed by the shell, which makes them faster than Python scripts that need to be interpreted by a Python interpreter. However, Python has several modules that can be used to optimize the code and improve performance.

D. Portability and compatibility:

Bash is available on most Unix-based systems and can be executed on any system that supports the Bash shell. In contrast, Python scripts may require the installation of Python and its dependencies on each system where the script will be executed, making it less portable. Additionally, Bash scripts may be more compatible with other shell commands and utilities used in the Unix shell environment.

E. Security and safety:

Python provides better error handling and a more secure execution environment than Bash. Bash scripts are more susceptible to shell injection attacks and other security vulnerabilities. Python's built-in security features, such as the capability to handle exceptions, prevent code injection, and other security features make it a more secure option for scripting tasks.

F. Maintenance and scalability:

Python's object-oriented approach, modular structure, and extensive libraries make it easier to maintain and scale than Bash. In contrast, Bash scripts can be more difficult to maintain as they tend to be more complex and lack the object-oriented structure that Python provides. Python scripts are more scalable as they can be easily extended and modified using libraries and modules.

Overall, these points highlight the key differences between Bash and Python scripting languages, making it easier for you to choose the appropriate language for your specific use case.

Congratulations! You have gotten to the end of the guide. Also, I’m currently in search of paid technical writing gigs related to cloud & devops, if you got one, reach out or refer me.

You can check out - My portfolio.

Top comments (2)

Collapse
 
jongeduard profile image
Eduard • Edited

"Bash scripts are directly executed by the shell, which makes them faster than Python scripts that need to be interpreted by a Python interpreter."

... but a shell is an interpreter. And the processing generally goes by one line at a time.
This article comes with a nice story, but with 0 benchmark results.

Collapse
 
devopsking profile image
UWABOR KING COLLINS

Awesome