Every DevOps engineer ends up typing the same commands again and again: check a service, tail a log, copy a build, restart something. A Bash script is simply those commands saved in a file so the computer can repeat them for you, the same way every time. In this lesson you will write, run and understand your first script, and learn what actually happens when you run it.
In simple words
- Bash: the most common Linux shell. It runs commands you type, and can also run them from a file.
- Script: a text file of commands that run in order, so you can repeat a task with one command.
- Shebang: the first line, such as #!/usr/bin/env bash, that says which program should run the script.
- Executable permission: the x permission that lets you run a file directly (chmod +x).
What Bash is
Bash (the Bourne Again SHell) is the program that reads what you type in a Linux terminal and runs it. It is the default shell on most Linux servers, inside most CI runners and in many container images. When you type ls and press Enter, Bash finds the ls program, runs it and shows you its output.
Bash is also a programming language. It has variables, conditions, loops and functions. That is why the same tool you use interactively is the one you use to automate.
Check which shell you are using and which version of Bash you have:
echo $SHELLbash --version/bin/bashGNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)Your first script, line by line
A script is a plain text file. Here is the smallest useful one:
#!/usr/bin/env bash# hello.sh - greet the current userecho "Hello, $USER"echo "Today is $(date +%A)"- Line 1, the shebang.
#!followed by a program path tells Linux which interpreter should run this file./usr/bin/env bashfindsbashwherever it is installed, which is more portable than hard-coding/bin/bash. - Line 2, a comment. Anything after
#is ignored. Use comments to explain why, not what. - Line 3 prints a greeting.
$USERis replaced by your user name. - Line 4 runs
date +%Aand puts its output (the weekday) into the sentence. This is called command substitution.
Create, make executable, run
In a real terminal you would open an editor (nano hello.sh or vim hello.sh). In the practice terminal you can create the same file with printf:
printf '#!/usr/bin/env bash\n# hello.sh - greet the current user\necho "Hello, $USER"\necho "Today is $(date +%%A)"\n' > hello.shcat hello.shNow try to run it:
./hello.shbash: ./hello.sh: Permission deniedNew files are not executable. Give the file execute permission once, then run it:
chmod +x hello.shls -l hello.sh./hello.sh-rwxr-xr-x 1 lynuxpro lynuxpro 96 Nov 28 09:42 hello.shHello, lynuxproToday is SundayThe x in -rwxr-xr-x means executable. You only need chmod +x once per file.
Why ./ is needed
When you type a command name like ls, Bash looks for it in the folders listed in $PATH. Your current folder is deliberately not in $PATH, so a file called ls in a download folder can never hijack the real ls. ./hello.sh means "the file hello.sh in this folder".
echo $PATHYou can also run a script by passing it to Bash directly. This works even without execute permission, because Bash is the program being run and the file is just its input:
bash hello.shWhat happens when you run a script
- You run
./hello.sh. - The kernel opens the file, sees
#!on the first line and starts/usr/bin/env bash hello.sh. - Bash reads the file top to bottom and runs each line exactly as if you had typed it.
- When the last line finishes, the script exits, and its exit code is the exit code of the last command.
Watch it run with tracing turned on. bash -x prints each command, after variables are replaced, with a + in front:
bash -x hello.sh+ echo 'Hello, lynuxpro'Hello, lynuxpro++ date +%A+ echo 'Today is Sunday'Today is SundayThis is the single most useful debugging tool in Bash, and you will use it all the time.
A script runs in its own process
A script runs in a child process. Anything it changes about its own environment, such as the current folder or a variable, disappears when it exits:
printf '#!/usr/bin/env bash\ncd /tmp\npwd\n' > go.shchmod +x go.sh./go.shpwd/tmp/home/lynuxproThe script was in /tmp, but your shell is still in your home folder. If you really want a file's commands to run in your current shell (for example to load variables), use source file (or its short form . file). You will see this with config files later.
Real-world scenario: the morning check
A platform engineer starts every day by checking the same things on a jump host: disk space, the last few error lines of the app log, and whether the hosts file still lists every server. Typing it all takes five minutes and something always gets forgotten. Saved as a script, it takes one second:
#!/usr/bin/env bash# morning.sh - daily sanity checkecho "== Disk"df -h /echo "== Last errors"grep ERROR /var/log/app/app.log | tail -n 3echo "== Hosts"cat /etc/hostsBuild and run it in the practice terminal:
printf '#!/usr/bin/env bash\necho "== Disk"\ndf -h /\necho "== Last errors"\ngrep ERROR /var/log/app/app.log | tail -n 3\necho "== Hosts"\ncat /etc/hosts\n' > morning.shchmod +x morning.sh./morning.shThe next step, which you will build in later lessons, is to let the script decide whether something is wrong and alert you only then.
Common mistakes
- Windows line endings. A script edited on Windows may end lines with
\r\n, giving errors like/usr/bin/env: 'bash\r': No such file or directory. Fix withdos2unix script.shorsed -i 's/\r$//' script.sh. - Missing shebang. Without it the file may be run by
shinstead of Bash, and Bash features break in confusing ways. - Spaces in the shebang path. It must be exactly
#!/usr/bin/env bashon the first line, with nothing before it, not even a blank line. - Forgetting
chmod +x. "Permission denied" almost always means this. - Naming a script
test.testis already a shell builtin, so typingtestruns that instead of your file. Use descriptive names ending in.sh.
Practice
- Create
whoami.shthat prints your user name, your home folder ($HOME) and the hostname (hostname). - Make it executable and run it with
./whoami.sh, then withbash -x whoami.shand compare. - Create
where.shthat runscd /var/logandls. After running it, check which folder your terminal is in, and explain why.
Cheat sheet
#!/usr/bin/env bash first line of every Bash script# comment ignored by Bashchmod +x script.sh make executable (once)./script.sh run from the current folderbash script.sh run without execute permissionbash -x script.sh trace every commandsource file / . file run in the current shellecho $? exit code of the last commandKey takeaways
- A Bash script is a text file of commands with a
#!/usr/bin/env bashshebang on line 1. - Make it executable once with
chmod +x, then run it with./name.sh. - Scripts run in a child process: their
cdand variables do not leak into your shell. bash -xshows exactly what a script is doing, line by line.