
Bash scripting is a powerful tool used to automate tasks and simplify system management in Linux systems. You can automate many operations such as server management, file operations, log analysis, and backups with a Bash script.
In this article, I will explain the basic structure of a Bash script, important commands, and advanced techniques.
1. What is Bash Script and Why Use It?
Bash (Bourne Again Shell) is a popular shell used in Linux systems. A Bash script is a file containing multiple commands that automate tasks.
Advantages of Using Bash:
- ✅ Automates manual tasks (backup, log cleaning, system updates).
- ✅ Accelerates repetitive tasks.
- ✅ Allows you to create your own management tools.
Bash script files are saved with the .sh
extension and can be run directly in Linux.
2. Basic Bash Commands
Before starting to write a Bash script, it is necessary to know some basic commands.
Simple Bash Script Example
#!/bin/bash
echo 'Hello, Bash Script!'
This script will output 'Hello, Bash Script!' in the terminal.
Running the Script:
- First, create the file:
BASH
nano first_script.sh
- Make it executable:
BASH
chmod +x first_script.sh
- Run the script:
BASH
./first_script.sh
3. Variables and Getting Input from the User
You can write dynamic scripts using variables in Bash.
Using Variables:
#!/bin/bash
name='Ahmet'
echo 'Hello, $name!'
Getting User Input:
#!/bin/bash
echo 'Enter your name:'
read user
echo 'Hello, $user!'
This script takes the user's name and prints it on the screen.
4. File and Directory Operations
You can automate file and directory operations with Bash scripts.
- Create File:
BASH
touch new_file.txt
- Delete File:
BASH
rm old_file.txt
- Create Directory:
BASH
mkdir new_folder
- List Files:
BASH
ls -lh
- Delete All Logs in a Specific Folder:
BASH
rm -rf /var/log/*.log
5. Conditional Statements and Loops
You can create decision-making processes using if
conditions and loops in Bash scripts.
Using If-Else
#!/bin/bash
echo 'Enter a number:'
read number
if [ $number -gt 10 ]; then
echo 'Number is greater than 10.'
else
echo 'Number is 10 or less.'
fi
Using For Loop
#!/bin/bash
for i in {1..5}
do
echo '$i. loop is running.'
done
This loop will print numbers from 1 to 5 on the screen.
6. Advanced Techniques
To make Bash scripts more advanced, you can use the following methods:
1️⃣ Run Script Automatically with Cron Job
Cron is used to run scheduled tasks in Linux.
To run a script every day:
crontab -e
Add the following line:
0 0 * * * /home/user/backup.sh
This will run the backup.sh script every night at 00:00.
2️⃣ Automatically Clean Log Files
Log files can grow large over time. You can create a script to clean them periodically:
#!/bin/bash
find /var/log -name '*.log' -type f -mtime +7 -exec rm -f {} \;
echo 'Old logs cleaned.'
This script will automatically delete all log files older than 7 days.
3️⃣ System Backup Script
The following script creates a backup by compressing files in a specific directory:
#!/bin/bash
date=$(date +%F)
tar -czf /backup/site_backup_$date.tar.gz /var/www/html/
echo 'Backup completed: site_backup_$date.tar.gz'
This script will create a backup of the website files every time it is run.
Bash scripts are powerful tools for automating tasks, simplifying system management, and saving time in Linux systems.
With Bash scripting, you can automate your system management and speed up your tasks!
For a detailed video tutorial, visit: Udemy Course
Related Articles

Advanced File and User Management in Linux
