TExcelTAD
Home › Linux & Bash › Bash Scripts

Bash Scripts

Typing commands one by one is fine for exploring, but as soon as you repeat a task you should put the commands in a script: a plain text file that Bash runs from top to bottom. Scripts are how Linux machines are configured, backed up, deployed and tested.

A first script

Create a file, give it a shebang line and run it with bash:

bash · try it yourself
cat > hello.sh <<'SCRIPT'
#!/usr/bin/env bash
# hello.sh: greet the user
echo "Hello, $(whoami)!"
echo "Today's tasks:"
echo " - learn Bash"
SCRIPT

bash hello.sh
Hello, learner!
Today's tasks:
 - learn Bash

cat > hello.sh <<'SCRIPT' ... SCRIPT writes the lines in between into hello.sh (a here-document, explained in its own lesson). In a real terminal you would usually write the file with an editor such as nano or VS Code.

The shebang line

The first line #!/usr/bin/env bash is the shebang. When you run a file directly (as ./hello.sh), the kernel reads that line to decide which interpreter to start. Common choices:

Shebang Meaning
#!/bin/bash Use Bash at a fixed path
#!/usr/bin/env bash Find bash on the PATH (more portable: works on macOS, NixOS, BSD)
#!/bin/sh A plain POSIX shell: Bash-only features such as arrays may not work
#!/usr/bin/env python3 The same mechanism works for any interpreter

Making it executable

To run a script by its name, it needs the execute permission. chmod +x adds it; then you run it with a path, ./hello.sh (./ means "in the current directory"):

bash · try it yourself
printf '#!/usr/bin/env bash\necho "running as a program"\n' > tool.sh
./tool.sh
echo "status: $?"
chmod +x tool.sh
./tool.sh
stat -c '%A %n' tool.sh
/w/main.sh: line 2: ./tool.sh: Permission denied
status: 126
running as a program
-rwxr-xr-x tool.sh

Without the x bit the first attempt fails with Permission denied (exit status 126). After chmod +x the file shows x in its permission string and runs.

Note: Why ./tool.sh and not just tool.sh? Bash only looks for bare command names in the directories listed in $PATH, and for safety the current directory is not one of them. The environment lesson shows how to install your own scripts into a ~/bin folder on the PATH.

Ways to run a script

Command What happens
bash script.sh Starts a new Bash that reads the file; no x permission needed
./script.sh Runs it as a program; needs x and a shebang
source script.sh or . script.sh Runs the lines in the current shell: variables and cd stay in effect afterwards

The difference between running and sourcing matters as soon as a script changes something:

bash · try it yourself
cat > setenv.sh <<'SCRIPT'
PROJECT=inventory
cd /tmp
SCRIPT

bash setenv.sh
echo "after bash:   PROJECT='$PROJECT' dir=$PWD"
source setenv.sh
echo "after source: PROJECT='$PROJECT' dir=$PWD"
after bash:   PROJECT='' dir=/home/learner
after source: PROJECT='inventory' dir=/tmp

The bash run happened in a child process: its variable and its cd vanished when it ended. source ran the same lines in our shell, so both changes stuck. This is exactly how ~/.bashrc and Python's venv/bin/activate work.

Script structure

A tidy script usually looks like this:

bash · try it yourself
cat > report.sh <<'SCRIPT'
#!/usr/bin/env bash
set -euo pipefail          # stop on errors (see the error-handling lesson)

readonly OUTPUT="report.txt"

main() {
  echo "Writing $OUTPUT"
  printf 'line %d\n' 1 2 3 > "$OUTPUT"
  echo "Done: $(wc -l < "$OUTPUT") lines"
}

main "$@"
SCRIPT

bash report.sh
cat report.txt
Writing report.txt
Done: 3 lines
line 1
line 2
line 3
  1. Shebang.
  2. Safety options.
  3. Constants at the top.
  4. Functions, with the main logic in a main function.
  5. A single call main "$@" at the bottom that passes along the script's arguments.

Debugging a script

Run a script with bash -x (or put set -x inside it) to print every command, prefixed with +, after expansion and before it runs:

bash · try it yourself
printf 'name="world"\necho "hello $name"\n' > debug.sh
bash -x debug.sh
+ name=world
+ echo 'hello world'
hello world

bash -n script.sh only checks the syntax without running anything, which is handy before deploying a script to a server.

Test yourself

What is the purpose of the first line #!/usr/bin/env bash?

  1. It is a comment and has no effect
  2. It tells the kernel which interpreter runs the file when executed directly
  3. It makes the file executable
  4. It imports Bash libraries

A script runs cd /tmp. Which way of running it leaves your shell in /tmp afterwards?

  1. bash script.sh
  2. ./script.sh
  3. source script.sh
  4. bash -x script.sh

Which option prints each command before it is executed?

  1. bash -n
  2. bash -x
  3. bash -e
  4. bash -v --quiet