If you type the same commands again and again, Linux offers a beautiful shortcut: write the commands once into a file, and let the computer run the whole list whenever you ask. That file is called a shell script, and learning bash scripting for beginners is the moment the terminal stops being a chore and starts working for you. In this guide you will write your first script, make it run, and teach it three real tricks: variables, decisions and loops.
What Is a Bash Script, in Plain English?
A Bash script is simply a text file containing commands, one per line, exactly as you would type them in the terminal. Bash (the shell behind most Linux terminals) reads the file from top to bottom and runs each command in order. That is all. There is no magic and no compiling.
Why bother? Because scripts are repeatable and reliable. A backup that takes eleven careful commands becomes one script you run with a single word, and it does the job the same way every single time. Most of the automation on Linux servers, including the backup routine in our guide to automating Linux backups with rsync and cron, is really just a script like the one you are about to write.
Bash Scripting for Beginners: Your First Script, Step by Step
Step 1: create the file. Open a text editor and create a file called hello.sh:
nano hello.sh
Step 2: write the script. Type these lines:
#!/bin/bash
# This is a comment. My first script!
echo "Hello from my first script"
echo "Today is:"
date
The first line, #!/bin/bash, is called the shebang. It tells Linux: “run this file with Bash.” Never leave it out. Lines starting with # are comments for humans; Bash ignores them. The echo commands print text, and date prints the current date and time. Save the file and exit the editor.
Step 3: make it executable. A fresh text file is not allowed to run as a program yet. Grant it permission:
chmod +x hello.sh
Step 4: run it. Type the path to the file:
./hello.sh
The ./ means “the file in this folder” — Linux will not run programs from the current folder without it, for security reasons. Congratulations, you have written and run your first script.

Bash Scripting for Beginners: Variables, If Statements and Loops
Variables store values for later. Assign with no spaces around the equals sign, and read with a dollar sign:
NAME="Alex"
echo "Welcome, $NAME"
You can also ask the person running the script for input with read: read -p "What is your name? " NAME stores whatever they type into NAME.
If statements let the script make decisions. This example checks whether a folder exists before using it:
if [ -d "$HOME/backups" ]; then
echo "Backup folder exists"
else
echo "Backup folder is missing"
fi
Notice the spaces inside the square brackets — they are required. The word fi (if backwards) closes the block.
Loops repeat work. A for loop runs a command for each item in a list:
for FILE in *.txt; do
echo "Found text file: $FILE"
done
And a while loop repeats as long as a condition stays true, for example counting from 1 to 3:
COUNT=1
while [ $COUNT -le 3 ]; do
echo "Count is $COUNT"
COUNT=$((COUNT + 1))
done
One more gift before we move on: your script can receive words typed after its name, called arguments. Inside the script, $1 means the first argument, $2 the second, and so on. A script containing echo "Backing up $1" and run as ./backup.sh Documents would print “Backing up Documents”. Arguments are how one small script becomes a flexible tool instead of a one-trick recording.
Combine these three ingredients — variables, decisions, loops — and you can build surprisingly useful little tools. This is the true heart of bash scripting for beginners: small pieces, patiently combined.
Common Beginner Mistakes
- Spaces around = in assignments.
NAME = "Alex"is an error. Always writeNAME="Alex"with no spaces. - Forgetting chmod +x. “Permission denied” when running your script almost always means you skipped this step.
- Forgetting the shebang. Without
#!/bin/bashthe file may run in the wrong shell or fail with odd errors. - Missing spaces in [ tests ].
[ -d "$HOME" ]needs those inner spaces;[-d "$HOME"]will not work. - Windows line endings. If you wrote the script on Windows, Linux may complain about a “bad interpreter”. Rewrite or convert the file on Linux itself.
Conclusion
Bash scripting for beginners boils down to four moves: create a text file, start it with #!/bin/bash, make it executable with chmod +x, and run it with ./script.sh. Then variables, if statements and loops turn your command list into a small program that can store values, decide and repeat. Start tiny, run your script after every change, and grow it one line at a time. When you are ready for the complete reference, the official GNU Bash manual covers every feature in detail.