Bash scripts Examples

```html Simple Bash Shell Script Examples

Simple Bash Shell Script Examples

Here are some simple Bash shell script examples to help you get started with your new skills. Each script includes a brief explanation of what it does.

1. Hello World

A basic script to print "Hello, World!" to the terminal.

#!/bin/bash # Print "Hello, World!" to the terminal echo "Hello, World!"

2. Greeting Script

A script that greets the user by name.

#!/bin/bash # Ask for the user's name echo "What is your name?" read name # Greet the user echo "Hello, $name!"

3. Basic Arithmetic

A script to perform basic arithmetic operations.

#!/bin/bash # Read two numbers from the user echo "Enter first number:" read num1 echo "Enter second number:" read num2 # Perform arithmetic operations sum=$((num1 + num2)) diff=$((num1 - num2)) prod=$((num1 * num2)) quot=$((num1 / num2)) # Display the results echo "Sum: $sum" echo "Difference: $diff" echo "Product: $prod" echo "Quotient: $quot"

4. File Existence Check

A script to check if a file exists and display a message.

#!/bin/bash # File to check file="example.txt" # Check if file exists if [ -e "$file" ]; then echo "File $file exists." else echo "File $file does not exist." fi

5. Loop Through Numbers

A script that loops through numbers 1 to 5 and prints each number.

#!/bin/bash # Loop through numbers 1 to 5 for i in {1..5} do echo "Number: $i" done

6. Countdown Timer

A script to create a simple countdown timer.

#!/bin/bash # Countdown from 10 to 1 for i in {10..1} do echo "$i" sleep 1 done echo "Time's up!"

7. Directory Listing

A script to list all files in a directory.

#!/bin/bash # Directory to list dir="." # List all files in the directory echo "Files in $dir:" ls "$dir"

8. Backup Script

A script to back up a directory.

#!/bin/bash # Source and destination directories src="/path/to/source" dest="/path/to/destination" # Create a backup cp -r "$src" "$dest" echo "Backup of $src completed to $dest"

9. Check Disk Space

A script to check the disk space usage.

#!/bin/bash # Check disk space usage echo "Disk space usage:" df -h

10. Simple To-Do List

A script to manage a simple to-do list.

#!/bin/bash # To-do list file todo_file="todo.txt" # Display menu echo "1. View to-do list" echo "2. Add a new item" echo "3. Exit" echo "Choose an option:" read option case $option in 1) if [ -e "$todo_file" ]; then cat "$todo_file" else echo "To-do list is empty." fi ;; 2) echo "Enter a new to-do item:" read item echo "$item" >> "$todo_file" echo "Item added." ;; 3) echo "Goodbye!" exit 0 ;; *) echo "Invalid option." ;; esac
```

VCF 9 Home Lab | Embedded vIDM (viDB) --- AD Integration, Users, Groups & NSX SSO

VCF 9 Home Lab | Embedded vIDM (viDB) — AD Integration, Users, Groups & NSX SSO 📅 May 2026  |  🏷️ VCF 9 Home Lab Series  ...