Bash Scripting Basics for Automation
If you find yourself running the same sequence of Linux commands repeatedly, it is time to write a Bash script. Bash (Bourne Again Shell) is the default command-line interpreter for most Linux systems, and scripting allows you to automate complex tasks.
Creating Your First Script
A Bash script is simply a plain text file containing a list of commands.
- Create a file: Create a new file, typically with a
.shextension (though the extension isn’t strictly necessary).touch myscript.sh - Add the Shebang: Open the file in an editor and add
#!/bin/bashas the very first line. This tells the system to use the Bash interpreter to execute the file. - Write your commands: Add the commands you want to run.
#!/bin/bash echo "Starting backup process..." tar -czf backup.tar.gz /home/user/documents echo "Backup complete!"
Making It Executable
Before you can run your script, you need to grant it execute permissions using the chmod command:
chmod +x myscript.sh
Now you can run it by specifying its path:
./myscript.sh
Variables and Control Structures
Bash scripting becomes powerful when you introduce variables and logic.
Variables:
#!/bin/bash
BACKUP_DIR="/var/backups"
echo "Saving to $BACKUP_DIR"
If/Else Statements:
#!/bin/bash
if [ -d "/var/www/html" ]; then
echo "Web directory exists."
else
echo "Web directory not found!"
fi
For Loops:
#!/bin/bash
for file in *.txt; do
echo "Processing $file"
done
Starting with simple scripts and gradually adding logic is the best way to master Linux automation.