Learn Understand first, then practice while the concept is still fresh.

M41 - Bash Scripting Essentials

Write and run small Bash scripts using a shebang, variables, conditionals, loops, and exit codes.

Scripting

Bash Scripting Essentials

Write and run small Bash scripts using a shebang, variables, conditionals, loops, and exit codes.

45 min INTERMEDIATE LINUX Curriculum-reviewed
What you should be able to do after this
  • Write and run small Bash scripts using a shebang, variables, conditionals, loops, and exit codes.

Bash as a Repeatable Linux Tool

Bash is the default scripting language many Linux learners meet first. It is close to the terminal you already know, which makes it useful for small automation, setup helpers, reports, and text-driven workflows.

Bash is not elegant in every detail, but it teaches very important operating-system habits:

  • paths matter
  • quoting matters
  • exit status matters
  • small tools work well together

1. The Shebang and Execute Permission

A Bash script is a text file. To run it directly, Linux needs two things:

  • a shebang telling the OS which interpreter should read the file
  • execute permission on the file itself
First Bash script

#!/bin/bash echo “Hello from Bash”

Save that as hello.sh, then run:

Make the file executable and run it

chmod +x hello.sh ./hello.sh

You can also run bash hello.sh, but learning chmod +x and ./script.sh helps you understand how the OS treats scripts as executable files.


2. Variables and Quoting

Bash variable syntax is strict. Do not put spaces around =.

Variables and quoting

WORKSPACE=“$HOME/os-practice” REPORT_NAME=“notes.txt”

echo “Workspace: $WORKSPACE” echo “Report: $REPORT_NAME”

Quotes matter because paths and input may contain spaces. Quoting variables keeps the shell from splitting one value into multiple arguments.


3. Conditionals and Exit Codes

Bash scripts often check whether a file or directory exists before acting.

Check before creating

WORKSPACE=“$HOME/os-practice”

if [ ! -d “$WORKSPACE” ]; then mkdir -p “$WORKSPACE” echo “Created $WORKSPACE” else echo “Folder already exists” fi

Commands also return an exit code when they finish:

  • 0 usually means success
  • non-zero usually means something failed

You can inspect the last exit code with $?:

Reading an exit code

ls “$HOME” echo $?

That number is how shell scripts often decide what to do next.


4. Loops for Repeated Checks

Once you can loop, you can turn a manual checklist into a reusable report.

Loop over files in a folder

TARGET=“$HOME”

for ITEM in “$TARGET”/*; do echo “Found: $ITEM” done

This works well for safe beginner tasks such as:

  • listing files to review
  • checking whether expected folders exist
  • collecting a simple report

5. A Small Script Pattern That Scales

A useful Bash script often follows the same pattern you learned earlier:

  1. define important values as variables
  2. test the current state
  3. do one clear action
  4. print a useful result

For example:

A small practical script

#!/bin/bash

WORKSPACE=“$HOME/os-practice” STAMP=”$(date +%F)”

mkdir -p “$WORKSPACE” echo “Study session on $STAMP” >> “$WORKSPACE/session-log.txt” echo “Saved note to $WORKSPACE/session-log.txt”

It is simple, but it is already more repeatable than typing those steps manually every time.

Best Beginner Habit

Quote variables unless you have a strong reason not to. In Bash, many strange bugs come from unquoted paths and values, especially when filenames include spaces.


What You Just Learned

  • A Bash script usually starts with #!/bin/bash.
  • chmod +x script.sh makes the file directly executable.
  • Bash variable assignment does not allow spaces around =.
  • Conditionals let the script inspect the system before changing it.
  • Exit codes tell you whether the last command succeeded.
  • Small Bash scripts are strongest when they do one clear job well.

Next, you will learn how scripting depends on streams, redirection, and text processing.