Bash scripting is a powerful tool for automating tasks and creating complex scripts in Linux. It is a versatile and widely-used scripting language that allows you to automate repetitive tasks, automate system administration, and perform other useful functions. In this post, we’ll go over 5 examples of bash scripts with the results you can expect, and a trick that you can use to improve your bash scripting.

  1. A simple bash script that prints “Hello, World!” to the terminal:
#!/bin/bash
echo "Hello, World!"

The result of this script will be the text “Hello, World!” printed on the terminal.

  1. A script that takes input from the user and greets them:
#!/bin/bash
echo "What is your name?"
read name
echo "Hello $name, nice to meet you!"

The script will prompt the user to enter their name, and then it will use the value entered to greet them with a personalized message.

  1. A script that lists the content of a directory:
#!/bin/bash
ls /home/user/documents

The script will execute the ls command on the specified directory and will list all the files and directories in the “documents” folder located in the “user” directory in the “home” directory.

  1. A script that checks the free space on a specific partition:
#!/bin/bash
partition="/dev/sda1"
freeSpace=$(df -h $partition | awk '{print $4}' | tail -n 1)
echo "Free Space on $partition is $freeSpace"

The script will check the available space on the specified partition and print the result in the terminal.

  1. A script that will create a backup of a specific folder:
#!/bin/bash
folderToBackup="/home/user/important_folder"
currentDate=$(date +%Y-%m-%d)
tar -czf "$folderToBackup-$currentDate.tar.gz" $folder