If you’ve ever found yourself typing the same commands over and over in the terminal, or if you want to automate a task on your computer, Bash scripting is here to save the day! A Bash script is like a recipe for your computer—it’s a file that contains a list of commands for your system to execute. In this guide, I’ll walk you through the process of creating your first Bash script, step by step. By the end, you’ll be able to write your own scripts to make your life easier.
What is a Bash Script?
A Bash script is a plain text file that contains a series of commands. When you run the script, your computer reads the file and executes the commands one by one. Think of it as a way to automate tasks you’d normally do manually in the terminal.
For example, instead of typing:
echo "Hello, World!"
ls
every time, you can save these commands in a script and run it with a single command.
Step 1: Open Your Terminal
To get started, open your terminal. On Linux or macOS, you can usually find it in your applications menu or by searching for “Terminal.” If you’re on Windows, you can use the Windows Subsystem for Linux (WSL) or a tool like Git Bash.
Step 2: Create a New Script File
Bash scripts are just text files, so you’ll need to create one. You can use any text editor, but for simplicity, we’ll use the terminal-based editor nano. Type the following command to create a new file:
nano myscript.sh
This opens the nano editor and creates a file named myscript.sh. The .sh extension is commonly used for Bash scripts, but it’s not required.
Step 3: Add the Shebang Line
The first line of your script should always be the shebang (#!). This tells your computer which program to use to run the script. For Bash scripts, the shebang is:
#!/bin/bash
Type this line at the top of your file. It’s like saying, “Hey computer, use Bash to run this script!”
Step 4: Write Your Commands
Now it’s time to add the commands you want your script to run. Let’s start with a simple example. Add the following lines to your script:
#!/bin/bash
# Print a message
echo "Hello, World!"
# List files in the current directory
ls
Here’s what each line does:
echo "Hello, World!"prints the text “Hello, World!” to the terminal.lslists all the files and folders in the current directory.
Step 5: Save and Exit
Once you’ve written your script, save the file and exit the editor. In nano, you can do this by:
- Pressing
CTRL + Xto exit. - Pressing
Yto confirm you want to save the changes. - Pressing
Enterto save the file with the namemyscript.sh.
Step 6: Make the Script Executable
By default, your script file isn’t executable, meaning you can’t run it directly. To fix this, you need to give it execute permissions. Run the following command in your terminal:
chmod +x myscript.sh
The chmod +x command tells your system, “This file is allowed to be executed.”
Step 7: Run Your Script
Now that your script is executable, you can run it! There are two ways to do this:
- Direct Execution:
./myscript.sh
The ./ tells your system to look for the script in the current directory.
- Using the Bash Interpreter:
bash myscript.sh
This method doesn’t require the script to be executable.
When you run the script, you should see:
Hello, World!
[list of files in the current directory]
Congratulations! You’ve just created and run your first Bash script.
Adding More Features to Your Script
Now that you’ve got the basics down, let’s make your script a little more advanced. Here are some common features you can add:
1. Variables
Variables let you store and reuse data. For example:
name="Alice"
echo "Hello, $name!"
This will print:
Hello, Alice!
2. User Input
You can ask the user for input using the read command:
echo "What's your name?"
read name
echo "Nice to meet you, $name!"
When you run this, it will wait for you to type your name and then respond.
3. Conditionals
Use if statements to make decisions in your script. For example:
if [ "$name" == "Alice" ]; then
echo "Welcome back, Alice!"
else
echo "Hello, stranger!"
fi
This checks if the name is “Alice” and responds accordingly.
4. Loops
Loops let you repeat commands. For example, a for loop:
for i in {1..5}; do
echo "Counting: $i"
done
This will print:
Counting: 1
Counting: 2
Counting: 3
Counting: 4
Counting: 5
Example: A Useful Backup Script
Let’s create a practical script that backs up a folder. Here’s what it looks like:
#!/bin/bash
# Variables
backup_dir="/backup"
source_dir="/home/user/documents"
timestamp=$(date +"%Y%m%d%H%M%S")
backup_file="$backup_dir/backup_$timestamp.tar.gz"
# Create backup directory if it doesn't exist
mkdir -p "$backup_dir"
# Create a compressed archive of the source directory
tar -czf "$backup_file" "$source_dir"
# Check if the backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully: $backup_file"
else
echo "Backup failed!"
fi
This script:
- Creates a backup directory if it doesn’t exist.
- Compresses the
source_dirinto a.tar.gzfile with a timestamp. - Checks if the backup was successful and prints a message.
Tips for Writing Good Bash Scripts
- Use Comments: Add comments (
#) to explain what your script does. This helps you and others understand the code later. - Test Your Script: Always test your script in a safe environment before using it for important tasks.
- Keep It Simple: Start small and add features gradually. Don’t try to do too much at once.
- Use Quotes: Always wrap variables in quotes to avoid issues with spaces or special characters.
Conclusion
You’ve just learned how to create a Bash script from scratch! Whether you’re automating tasks, managing files, or just having fun, Bash scripting is a powerful tool to have in your toolkit. Start with simple scripts, experiment with new features, and soon you’ll be writing scripts like a pro. Happy scripting! 🎉

