Skip to content
$ lynuxpro
← Bash Scripting

LESSON 1 OF 17 · BEGINNER · 14 MIN

Your first Bash script

HOW IT WORKSFrom a text file to a running script

hello.shplain text
1/5

A script is just a text file of commands. The first line, the shebang, says which program should run it.

#!/usr/bin/env bash
echo "Hello, $USER"

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
SHELLbashutf-8 · 2L
/bin/bashGNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
OUTPUTterminalutf-8 · 2L

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)"
SHELLbashutf-8 · 4L
  • Line 1, the shebang. #! followed by a program path tells Linux which interpreter should run this file. /usr/bin/env bash finds bash wherever 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. $USER is replaced by your user name.
  • Line 4 runs date +%A and 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.sh
SHELLbashutf-8 · 2L

Now try to run it:

./hello.sh
SHELLbashutf-8 · 1L
bash: ./hello.sh: Permission denied
OUTPUTterminalutf-8 · 1L

New files are not executable. Give the file execute permission once, then run it:

chmod +x hello.shls -l hello.sh./hello.sh
SHELLbashutf-8 · 3L
-rwxr-xr-x 1 lynuxpro lynuxpro    96 Nov 28 09:42 hello.shHello, lynuxproToday is Sunday
OUTPUTterminalutf-8 · 3L

The 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 $PATH
SHELLbashutf-8 · 1L

You 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.sh
SHELLbashutf-8 · 1L

What happens when you run a script

  1. You run ./hello.sh.
  2. The kernel opens the file, sees #! on the first line and starts /usr/bin/env bash hello.sh.
  3. Bash reads the file top to bottom and runs each line exactly as if you had typed it.
  4. 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
SHELLbashutf-8 · 1L
+ echo 'Hello, lynuxpro'Hello, lynuxpro++ date +%A+ echo 'Today is Sunday'Today is Sunday
OUTPUTterminalutf-8 · 5L

This 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
SHELLbashutf-8 · 4L
/tmp/home/lynuxpro
OUTPUTterminalutf-8 · 2L

The 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/hosts
SHELLbashutf-8 · 8L

Build 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.sh
SHELLbashutf-8 · 3L

The 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 with dos2unix script.sh or sed -i 's/\r$//' script.sh.
  • Missing shebang. Without it the file may be run by sh instead of Bash, and Bash features break in confusing ways.
  • Spaces in the shebang path. It must be exactly #!/usr/bin/env bash on 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. test is already a shell builtin, so typing test runs that instead of your file. Use descriptive names ending in .sh.

Practice

  1. Create whoami.sh that prints your user name, your home folder ($HOME) and the hostname (hostname).
  2. Make it executable and run it with ./whoami.sh, then with bash -x whoami.sh and compare.
  3. Create where.sh that runs cd /var/log and ls. 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 command
OUTPUTterminalutf-8 · 8L

Key takeaways

  • A Bash script is a text file of commands with a #!/usr/bin/env bash shebang on line 1.
  • Make it executable once with chmod +x, then run it with ./name.sh.
  • Scripts run in a child process: their cd and variables do not leak into your shell.
  • bash -x shows exactly what a script is doing, line by line.