OSBash.001: Shell and Script Basics — Bash, Shebangs, chmod, ./, and Your First Script

A programmer working at a laptop in a warm modern office.

Elementary Overview

Bash is both a command shell and a scripting language. You can type commands interactively into a terminal, or you can save a sequence of commands in a text file and run that file as a script. A Bash script is useful when you want the computer to repeat a procedure consistently instead of relying on you to type every command by hand.

This first Bash lesson stays deliberately narrow. You will learn what a shell is, what makes Bash different from the terminal window itself, how a script file works, what a shebang does, why executable permission matters, and why ./script.sh is different from simply typing a command name. Later Bash lessons will slow down and focus separately on commands and paths, variables, quoting, pipes, redirection, conditionals, loops, functions, arguments, exit codes, text-processing tools, permissions, processes, environment variables, scheduling, SSH automation, logging, and safe error handling.

What You Should Learn

  • What a shell does and where Bash fits.
  • The difference between an interactive command and a saved shell script.
  • How to create a first Bash script.
  • What the #! shebang line means.
  • How to run a script with bash script.sh.
  • How to make a script executable with chmod +x.
  • Why ./script.sh specifies a path to the current directory.
  • Why the shell searches PATH when you type a command name without a slash.
  • How to read the first common Bash execution errors without guessing.

What A Shell Is

A shell is a program that reads commands and starts other programs. Bash stands for Bourne Again Shell. It grew out of the Unix shell tradition and remains one of the most widely encountered shells on Linux systems, servers, containers, development environments, and infrastructure tooling.

The terminal window is not Bash itself. A terminal application gives you a text interface. Inside that terminal, a shell such as Bash, Zsh, or another command interpreter reads what you type. That distinction becomes important when a script behaves differently under bash than it does under sh or another shell.

When the shell launches another program, the operating system participates in the process-creation and execution path. The earlier BitcoinVersus.Tech explainer What Are fork() and exec()? provides useful background on how Unix-like systems create and launch programs beneath the command line.

NetworkChuck introduces Bash scripting, the shell, shebangs, script execution, and executable permissions in a practical first lesson.
A programmer typing on a laptop in a bright plant-filled workspace.
Original BitcoinVersus.Tech lesson image for OSBash.001.

Interactive Commands Versus Scripts

If you type a command directly at a Bash prompt, Bash reads and executes it interactively. For example:

pwd
ls
echo "Hello"

A script stores commands in a file so the same sequence can be run again. The GNU Bash manual defines a shell script as a text file containing shell commands. That simple idea is the foundation of automation: commands that work interactively can often be organized into a repeatable script.

Create Your First Script

Create a file named hello.sh in a working directory. Use any plain-text editor you are comfortable with. If you are new to code editors, the BitcoinVersus.Tech lesson What Is Syntax Highlighting? explains why editors visually distinguish commands, strings, comments, and other syntax.

#!/usr/bin/env bash

echo "Hello from Bash"

The second line runs the Bash builtin echo, which writes text to standard output. The first line is more unusual. It begins with the two characters #!, commonly called a shebang.

freeCodeCamp.org — “Bash Scripting Tutorial for Beginners.” The course begins with basic commands and a first Bash script before moving into later scripting features.

What The Shebang Does

When a script is executed directly, the shebang tells the operating system which interpreter should handle the file. In this example:

#!/usr/bin/env bash

env searches the current environment’s PATH for a program named bash. Another common form is #!/bin/bash, which names a specific path directly. Which form is appropriate depends on the environment and deployment requirements. The important beginner rule is simpler: the shebang identifies the intended interpreter when the script is executed as a program.

If you explicitly run bash hello.sh, you are already choosing Bash on the command line, so Bash can read the file even if it is not marked executable. Direct execution through ./hello.sh is different because the file itself is being launched as a program.

r/bash discussion: why an executable script with a shebang behaves differently from explicitly running the same file with bash script.sh.

Run The Script With Bash

The most direct beginner test is:

bash hello.sh

This starts Bash and gives the script file to Bash as input. The file needs to be readable, but it does not need its executable permission bit set just to be read this way.

If the command works, you have already proved several things: Bash exists, the shell can find the Bash executable, the file is readable, and the script syntax is valid enough for Bash to execute it.

Make The Script Executable

To launch the file directly as a program, add executable permission:

chmod +x hello.sh

Then run it with a path:

./hello.sh

chmod +x changes the file’s mode so the operating system can treat it as executable for the applicable permission classes. Permissions deserve their own later lesson; for now, remember the practical sequence: create the script, add a shebang, make the file executable, then launch it with a path.

TechWorld with Nana — “Bash Scripting Tutorial for Beginners.” The walkthrough covers shells, Bash, the first script, file extensions, shebangs, formatting, and later automation concepts.

Why ./ Matters

The characters ./ are a path. A single dot means the current directory, so ./hello.sh means “execute the file named hello.sh from this directory.”

If you type only hello.sh, Bash normally treats that as a command name and searches the directories listed in the PATH environment variable. The current directory is commonly not included in PATH by default, which is why a local script may work with ./hello.sh but not with hello.sh.

For a deeper explanation, read What Is the PATH Environment Variable?. Later in this Bash track, commands and paths will get a dedicated lesson rather than being overloaded into this introduction.

Comments And Blank Lines

In Bash, a line beginning with # is normally a comment. Comments are ignored as shell commands and are useful for explaining why a script exists or what a section is doing.

#!/usr/bin/env bash

# Print a simple greeting.
echo "Hello from Bash"

The shebang is a special first-line case recognized by the operating system when the file is executed. Blank lines are also useful because they visually separate logical sections without changing normal execution.

Read Errors In A Fixed Order

  1. bash: command not found: verify Bash is installed and discoverable through PATH.
  2. No such file or directory: verify your current directory, the filename, and the path you typed. Also inspect the shebang if direct execution fails unexpectedly.
  3. Permission denied with ./script.sh: inspect the file’s execute permission.
  4. Works with bash script.sh but not ./script.sh: check executable permission and the shebang.
  5. Works in Bash but fails under sh: the script may use Bash-specific syntax. Run it with the interpreter it was written for instead of assuming every shell is interchangeable.

This habit matters more than memorizing one error message. Start with the command you typed, the path, permissions, interpreter, and first useful diagnostic. Avoid changing several unrelated things at once.

Mini Lab

  1. Create a directory named bash_lab and enter it.
  2. Create hello.sh containing a Bash shebang and one echo command.
  3. Run it with bash hello.sh.
  4. Run ls -l hello.sh and inspect the permissions.
  5. Add executable permission with chmod +x hello.sh.
  6. Run it with ./hello.sh.
  7. Remove the executable bit and observe what changes for direct execution.
  8. Restore executable permission.
  9. Temporarily misspell the filename and read the exact error instead of guessing.
  10. Commit the working script to a small repository if you are practicing Git and GitHub.

Common Beginner Mistakes

  • Confusing the terminal with Bash: the terminal hosts a shell; Bash is one possible shell.
  • Assuming every .sh file is Bash: the extension does not determine the interpreter by itself.
  • Forgetting the shebang: direct execution needs a clear interpreter path if the operating system is expected to choose the interpreter.
  • Forgetting executable permission: bash script.sh and ./script.sh are not identical execution paths.
  • Typing only a local filename: command lookup uses PATH; ./ explicitly names the current directory.
  • Running a Bash script with sh: Bash-specific syntax is not guaranteed to work under another shell.
  • Editing several things after one error: change one variable at a time so you know what fixed the problem.

Exercises

  1. Explain the difference between a terminal and a shell.
  2. Create a two-line Bash script that prints your chosen message.
  3. Run the script once with bash filename and once through direct execution.
  4. Use ls -l before and after chmod +x and identify the changed permission bit.
  5. Explain what ./ means in ./hello.sh.
  6. Explain why typing only hello.sh may produce “command not found.”
  7. Change the shebang to an invalid path, attempt direct execution, record the error, and restore the correct shebang.
  8. Write one comment in a script that explains intent rather than repeating the command.

Knowledge Check + Answers

  1. What is Bash? A Unix-style command shell and scripting language.
  2. Is a terminal window the same thing as Bash? No. A terminal provides the text interface in which a shell such as Bash can run.
  3. What is a Bash script? A text file containing commands intended to be read and executed by Bash.
  4. What does a shebang do? It identifies the interpreter intended to execute the file when the script is launched directly.
  5. Does bash hello.sh require the file to have its executable bit set? No. Bash reads the file as input.
  6. What does chmod +x hello.sh do? It adds executable permission according to the applicable file mode classes.
  7. What does ./hello.sh mean? Execute hello.sh using the path to the current directory.
  8. Why might hello.sh alone fail? The shell searches PATH, and the current directory may not be in that search path.
  9. Should a Bash-specific script automatically be run with sh? No. Use the interpreter the script was written and tested for.

Primary References

Elementary Review

A Bash script is simply a text file containing shell commands, but execution details matter. The shebang identifies the intended interpreter for direct execution. bash script.sh explicitly starts Bash and asks it to read the file. chmod +x makes direct execution possible, and ./ tells the shell exactly where the local script is. Once those mechanics make sense, later Bash syntax becomes much easier to troubleshoot.

The next canonical Bash lesson is OSBash.002: Commands and Paths, where command lookup, absolute paths, relative paths, PATH, current directories, and executable discovery can be studied in detail.

Editor’s Note

The featured image is an original 1200×630 BitcoinVersus.Tech photograph created specifically for OSBash.001 and is not reused in the body. The lesson uses a separate original 1200×675 body photograph. The three YouTube videos use responsive native Gutenberg 16:9 embed blocks, and the social item uses a responsive native Gutenberg embed. Ordinary lesson prose is not placed inside bordered, shaded, card, callout, panel, or fixed-width text boxes.

BitcoinVersus.Tech content is provided for informational and educational purposes.

Leave a Reply