How to Write Your First Bash Script
Write your first bash script step by step: the #! line, making it runnable, variables, arguments, if checks and loops, then a script that checks your logs.

On this page 4 sections ▾
TL;DR
- Give a script execute permission with
chmod +xto run it as./script.sh. The#!/bin/bashfirst line makes sure bash is what runs it. - Variables have no spaces around the
=.name="Graham"works;name = "Graham"doesn't. - End a script with
exit 1when something goes wrong, so other scripts and tools can tell it failed.
If you've typed the same few commands more than twice, it's time to write your first bash script. Put them in a file once, and from then on one short command runs the whole lot, in order, the same way every time. By the end of this you'll have a script that checks a folder of logs for errors.
Quick facts
The .sh on the end of a script is only a naming habit. What tells Linux how to run the file is its first line.
bash -x script.sh prints every line just before it runs it, with the variables already filled in. It's the quickest way to see where a script goes wrong.
#1. Write the bash script file
A script is a plain text file. Open one in any editor, for example with nano hello.sh, and put this inside. The first line, starting with #!, names the program that should run everything below it. Lines starting with # after that are comments for humans and are ignored.
| 1 | #!/bin/bash |
| 2 | # My first script |
| 3 | echo "Hello from my first bash script" |
#2. Make it runnable and run it
A new file isn't allowed to run as a program yet. chmod +x turns on its execute permission, and ./ tells the shell to look for the file in the folder you're in. bash hello.sh works even without that step, because bash opens and reads the file itself.
student@opnf:~$ ./hello.sh bash: ./hello.sh: Permission denied student@opnf:~$ chmod +x hello.sh student@opnf:~$ ./hello.sh Hello from my first bash script student@opnf:~$ bash hello.sh Hello from my first bash script
#3. Add variables and arguments
A variable stores a value under a name, and you read it back with a $ in front. Anything you type after the script's name arrives as $1, $2 and so on. ${1:-friend} means: use the first argument, or the word friend if there isn't one. $(...) runs a command and drops its output into the line.
| 1 | #!/bin/bash |
| 2 | name="${1:-friend}" |
| 3 | today=$(date +%A) |
| 4 | echo "Hi $name, happy $today." |
student@opnf:~$ ./greet.sh Hi friend, happy Saturday. student@opnf:~$ ./greet.sh Graham Hi Graham, happy Saturday.
ELI5: explain it simply▾

THE WORDS
- Variable
- A labeled box that holds a value, like a name or a number, so you can use it later.
- Argument
- Extra information you type after a command's name, like Graham in ./greet.sh Graham.
- $1
- Where the script finds the first argument you gave it.
THE IDEA
Think of the script as a form letter with a blank for the name. If you hand it a name when you run it, it fills the blank in. If you don't, it uses the backup word written next to the blank.
The space trap
No spaces around =. With them, bash thinks name is a command you're trying to run, prints "name: command not found" and carries on with an empty variable.
student@opnf:~$ cat oops.sh #!/bin/bash name = "Graham" echo "Hi $name" student@opnf:~$ ./oops.sh ./oops.sh: line 2: name: command not found Hi
When a script misbehaves and the error isn't this obvious, run it with bash -x. Each line it runs is printed with a + in front, so you can see exactly what each variable held at that moment.
student@opnf:~$ bash -x greet.sh Graham + name=Graham ++ date +%A + today=Saturday + echo 'Hi Graham, happy Saturday.' Hi Graham, happy Saturday.
#4. Make decisions and repeat work
This is where a script starts earning its keep. if checks something before going on, here whether the folder exists, and exit 1 stops the script with a failure code if it doesn't. A for loop then runs the same lines once for every file that matches *.log.
| 1 | #!/bin/bash |
| 2 | # Count the error lines in every .log file in a folder |
| 3 | dir="${1:-.}" |
| 4 | |
| 5 | if [ ! -d "$dir" ]; then |
| 6 | echo "No folder called $dir" |
| 7 | exit 1 |
| 8 | fi |
| 9 | |
| 10 | for file in "$dir"/*.log; do |
| 11 | count=$(grep -c "error" "$file") |
| 12 | echo "$file: $count error lines" |
| 13 | done |
student@opnf:~$ ./logcheck.sh logs logs/app.log: 1 error lines logs/db.log: 0 error lines logs/web.log: 2 error lines student@opnf:~$ ./logcheck.sh nowhere No folder called nowhere student@opnf:~$ echo $? 1 # $? holds the exit code of the last command: 0 means success, anything else means it failed
Where it breaks
Point it at a folder with no .log files and bash leaves *.log as plain text, so grep complains that empty/*.log doesn't exist. Adding [ -e "$file" ] || continue as the first line inside the loop skips that case. We tested both versions.
Tip
Wrap variables in double quotes, like "$file". Without them, a file name with a space in it gets split into two words and the command breaks.

Write scripts hands-on
Shell scripting is part of DevOps & Infrastructure, Level 3 of DevOps Pro, where you write and run scripts step by step with labs and quizzes in a simulated terminal.
What is a bash script?
How do I run a .sh file in Linux?
chmod +x and run it with ./file.sh, as in step 2. From another folder, give its path instead, like ~/scripts/file.sh.What does #!/bin/bash mean?
./script.sh runs under bash no matter which shell you start it from.What's the difference between bash and sh?
#!/bin/sh may be run by a simpler shell that doesn't understand bash-only features, so use #!/bin/bash for bash scripts.Get opnF in your inbox
Tech news for IT professionals that actually teaches you something.
Our promise to you: we'll be conscious about email quality/frequency, and your address will never be shared/sold.