Bash Scripting
Bash (Bourne Again SHell) is the default shell for most Linux distributions. Learning to write bash scripts allows you to automate repetitive tasks, create custom commands, and build powerful tools. This guide will introduce you to the basics of bash scripting.
Getting Started with Bash Scripts
A bash script is a plain text file containing a series of commands. Here's how to create and run your first script:
1. Create a Script File
Use any text editor to create a file with the .sh extension:
$ nano myscript.sh
2. Add the Shebang Line
The first line of a bash script should be the shebang, which tells the system which interpreter to use:
#!/bin/bash
3. Add Commands
Add commands to your script:
#!/bin/bash
# This is a comment
echo "Hello, World!"
echo "My first bash script"
# Create a variable
name="Linux User"
echo "Hello, $name!"
4. Make the Script Executable
$ chmod +x myscript.sh
5. Run the Script
$ ./myscript.sh
Variables in Bash
Variables allow you to store and manipulate data in your scripts:
#!/bin/bash
# Define variables
name="John"
age=30
is_student=true
# Access variables with $
echo "Name: $name"
echo "Age: $age"
echo "Student: $is_student"
# Command substitution
current_date=$(date)
echo "Current date: $current_date"
# Arithmetic operations
result=$((age + 5))
echo "$name will be $result in 5 years"
Conditional Statements
Conditionals allow your scripts to make decisions:
If-Else Statement
#!/bin/bash
# If-else statement
age=20
if [ $age -ge 18 ]; then
echo "You are an adult"
else
echo "You are a minor"
fi
# File test operators
if [ -f "config.txt" ]; then
echo "config.txt exists"
else
echo "config.txt does not exist"
fi
Common Test Operators
| Operator |
Description |
Example |
| -eq |
Equal to |
[ $a -eq $b ] |
| -ne |
Not equal to |
[ $a -ne $b ] |
| -gt |
Greater than |
[ $a -gt $b ] |
| -lt |
Less than |
[ $a -lt $b ] |
| -f |
File exists and is a regular file |
[ -f $file ] |
| -d |
File exists and is a directory |
[ -d $directory ] |
Loops
Loops allow you to repeat actions multiple times:
For Loop
#!/bin/bash
# Basic for loop
for i in 1 2 3 4 5
do
echo "Number: $i"
done
# Loop through a range
for i in {1..5}
do
echo "Range number: $i"
done
# Loop through files
for file in /etc/*.conf
do
echo "Found config file: $file"
done
While Loop
#!/bin/bash
# While loop
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count + 1))
done
Functions
Functions allow you to organize your code into reusable blocks:
#!/bin/bash
# Define a function
greet() {
echo "Hello, $1!"
echo "Today is $(date)"
}
# Call the function with a parameter
greet "User"
# Function with return value
calculate_sum() {
local num1=$1
local num2=$2
local sum=$((num1 + num2))
return $sum
}
calculate_sum 5 7
result=$?
echo "Sum: $result"
Next Steps
Now that you've learned the basics of bash scripting, you might want to explore:
- Advanced string manipulation
- Regular expressions in bash
- Error handling and debugging
- Handling command line arguments
- Creating interactive scripts
Practice is key to mastering bash scripting. Try to create scripts to automate tasks you perform regularly.