$_ CLI In Depth
Learning progress 0%
lesson scripting exit-codes control-flow

Your First Script & the Shebang

7 min read Module 13: Scripting Basics & Exit Codes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:21

From typed commands to a file

Everything you type at the prompt can go into a file. The file is a script, and it is nothing more than plain text. Bash reads the file one line at a time, starting at the top, and runs each command the same way it would run a command typed by hand. Blank lines are ignored. A hash sign starts a comment that runs to the end of the line, so you can describe what the script does for the next reader. Comments cost nothing at run time, and they are the cheapest documentation a script can have.

Scripts are often saved with a .sh extension. The extension is a convention for people and editors, not a rule for the system. Linux does not care about the name, so a script called deploy with no extension works just as well. What matters is the content of the first line and the permission bits on the file.

BASH5 lines
cat > hello.sh << 'EOF'
#!/bin/bash
# Print one sentence.
echo "hello from a script"
EOF

The shebang line

The first unit of this module has one more practical point. When you write a script, you choose the interpreter, and that choice is part of the script behavior. A script that uses bash-only features such as double brackets must name bash in its shebang. A script that is meant to run under any POSIX shell should avoid those features, and should name sh. Writing the shebang deliberately makes that promise visible to the next reader.

The first two characters of a script can be #!. Together they are called the shebang, or hash-bang. When the kernel is asked to run a file, it looks at those two bytes. If they are present, the rest of the first line is treated as the path to an interpreter, and the kernel starts that interpreter with the script file as its argument. The line #!/bin/bash therefore means that bash should run the file.

Bash itself treats the shebang as an ordinary comment, because it begins with a hash sign. The line matters only to whoever starts the file. For the same reason, you can write a shebang for any language, such as #!/usr/bin/env python3, and the kernel will hand the file to Python.

The path in the shebang is absolute, so /bin/bash has to exist at that location, and most Linux systems put bash there. The form #!/usr/bin/env bash asks env to search PATH for bash instead. That form is more portable, because bash can live in a different place on some systems, such as a Homebrew install on macOS. The lab for this course puts bash at /bin/bash, so either form works there.

Two details trip people up. The first is line endings. A file edited on Windows may end each line with a carriage return followed by a line feed. The shebang then names /bin/bash followed by a carriage return, and the error says no such file or directory, even though bash exists. The fix is to save the file with Unix line endings. The second is length. The kernel reads only the first part of the file to find the interpreter, so keep the shebang short.

Two ways to run a script

Bash can run a script in two ways, and they behave differently. The first is to pass the file to bash as an argument, as in bash hello.sh. Here you name the interpreter yourself, so the shebang is ignored, and the file does not need the execute permission. Bash only needs to read the file. This form is useful for a quick test, and it works even on a file with no permission bits set.

The second way is to run the file by its path. The file must carry the execute permission for you, and the kernel then reads the shebang and starts the interpreter. You make a file executable with chmod. The usual pair of commands looks like this:

BASH2 lines
chmod +x hello.sh
./hello.sh

The mode column of ls -l shows the permissions. A runnable file shows an x in the owner position, and after chmod you should see something like -rwxr-xr-x. The execute bit is not about the contents. A file without the bit set cannot be started by its path, however good its contents are, and a file with the bit set is still just text that the kernel hands to an interpreter.

If you forget chmod, the path form fails with a clear message. Bash prints a permission error and returns exit status 126, which means the command was found but could not be run. Status 126 and its neighbors are covered in the exit codes unit of this module.

Note

The two forms can give different results for the same file. A file with the wrong shebang can still run with bash file, because the shebang is never read. Running it by path would then fail. When a script works one way and not the other, compare the shebang with the interpreter you expect.

Why the ./ prefix matters

Bash looks up a command name without a slash by searching the directories listed in PATH, and the current directory is usually not one of them. So typing hello.sh alone fails, even when the file is executable. The message is command not found, and the exit status is 127. The prefix ./ tells bash that the name is a path relative to the current directory. You can also type a full path, such as /workspace/hello.sh, which has the same effect.

Adding the current directory to PATH is possible, but it is a security risk. Anyone who can write to a directory in your PATH can place a program there that shadows a real command. Typing ./ is a small habit that avoids the risk and also makes it obvious which file runs.

Where the script runs

When you run a script, the kernel starts a new process for it. That process is a separate bash, and it has its own variables, its own working directory, and its own exit status. Variables set in the script do not appear in your shell afterward, and a cd inside the script does not move you. The script exit status becomes the status of the command you ran, which is what $? reports.

That isolation is useful. A script can change its variables freely without damaging the environment of the shell that started it. When you do want the variables to remain, you use the source command, which runs a file inside the current shell. A later module covers sourcing files in detail.

Hands-on: write and run one script

Create a script in /workspace, which is your home directory in the lab. The block below writes the file with a here-document, a way to put several lines into one command without opening an editor.

BASH4 lines
cat > hello.sh << 'EOF'
#!/bin/bash
echo "hello from a script"
EOF

Run it first with bash hello.sh. Then make it executable with chmod +x hello.sh and run it with ./hello.sh. Both runs print the same sentence. Next, remove the execute bit with chmod -x hello.sh and try ./hello.sh again. You should see the permission error. Run echo $? on the next line to confirm the status 126.

Most scripts need more than a fixed sentence. When a script receives values from the command line, it reads them as positional parameters, and the next unit covers those in detail.

How the kernel runs a script

Visualizer

A script is only a file. The kernel decides how to run it from its first bytes: a #! line names an interpreter, and a file without one falls back to the shell.

1 / 5
execve() on ./hello.sh
kernel reads the first bytes
sees #! and parses the line
argv rebuilt, interpreter started
bash reads the script
$ ./hello.sh a b

You type ./hello.sh a b. Bash forks a child, and the child calls execve() on the file, passing the words as argv.

printf '#!/bin/bash\necho "argv0=$0 args=$*"\n' > /tmp/shebang-demo.sh; chmod +x /tmp/shebang-demo.sh; /tmp/shebang-demo.sh a b
Terminal lab · 2 steps · real Linux in your browser

Write and run a script

Create a script with a shebang line, then make it executable so it runs by its path.

01
Write a script with a shebang
Create hello.sh in /workspace. The first line is the shebang, which names bash. The second line prints one sentence: ``bash cat > hello.sh <<'EOF' #!/bin/bash echo "hello from a script" EOF `` Run it with bash hello.sh and confirm that the sentence appears.
Show hint for step 1
The first two characters of the first line must be #! followed by the path to bash. The printed sentence must match exactly, including the capital letters.
02
Make the script executable
Running a file by its path needs the execute permission bit. Add it to hello.sh with chmod, then run the file directly: ``bash chmod +x hello.sh ./hello.sh `` The output is the same sentence. Without chmod, the same command fails with Permission denied and exit status 126.
Show hint for step 2
chmod +x adds the execute bit for everyone. The ./ prefix runs the file in the current directory, which is not in PATH.
Check yourself Question 1 of 4
Concept Check Characters that start a shebang recall

Which two characters must be the first two bytes of a script for the kernel to treat the first line as an interpreter line?

Recap & Key Takeaways

The shebang on the first line names the interpreter, and the kernel uses it only when the file is run by its path.
Running a script as bash file ignores the shebang and does not need the execute bit, while running it as ./file needs both the bit and a valid shebang.
A name without a slash is searched in PATH, so ./ is needed for a script in the current directory, and the failure statuses are 126 and 127.
A script runs in its own process, so its variables and directory changes do not persist in your shell.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue