Skip to content

Chapter 1 of 8

Foundations of Bash Scripting

Bash, the Bourne Again SHell, is a Unix shell and command language written as a free software replacement for the original Bourne shell. It serves as the default login shell on most Linux distributions and, before macOS Catalina, on Apple's desktop operating systems as well. Because Bash is installed almost everywhere a Unix-like system runs, it is the natural choice for writing portable automation scripts that glue other tools together.

A Bash script is simply a text file containing commands the shell can interpret. By convention such files carry the .sh extension to signal intent and let editors and tooling recognize them as shell scripts, although the extension is not strictly required for execution. The first line of an executable Bash script must be a shebang — a line such as #!/usr/bin/env bash or #!/bin/bash — which tells the kernel which interpreter to invoke when the file is run directly. #!/usr/bin/env bash is often preferred because it locates Bash through the user's PATH, which keeps the script working even when Bash lives in a non-standard location.

Once the file has the right shebang, it must be made executable before it can be launched as a program. The command chmod +x script.sh (or the equivalent chmod 755 script.sh) sets the execute bit. To run a script in the current directory you invoke it with ./script.sh; the leading ./ is required because the current directory is not normally included in PATH. There is an important distinction between sh script.sh and ./script.sh: the former explicitly invokes the sh interpreter and ignores the shebang, so Bash-only features may break, while the latter honors the shebang and uses the interpreter declared on the first line. To guarantee Bash regardless of how a script is invoked, you can guard at runtime with [[ -n "${BASH_VERSION:-}" ]] || { echo "Need bash"; exit 1; }.

All chapters
  1. 1Foundations of Bash Scripting
  2. 2Variables, Expansion, Quoting, and Strict Mode
  3. 3Conditionals and Tests
  4. 4Loops and Iteration
  5. 5Functions, Sourcing, and Command-Line Arguments
  6. 6Arrays, Strings, Substitutions, and Arithmetic
  7. 7Input/Output, Redirection, and File Handling
  8. 8Process Control, Scheduling, and Robust Practices

Drill it

Reading is not remembering. These come from the Bash Scripting For Automation deck:

Q

What is Bash?

Bash (Bourne Again SHell) is a Unix shell and command language written as a free software replacement for the Bourne shell (sh). It is the default login shell o...

Q

What file extension is conventionally used for Bash scripts?

.sh. While not strictly required for execution, .sh signals intent and lets tooling treat the file as a shell script.

Q

What must the first line of an executable Bash script be?

A shebang line, e.g. #!/usr/bin/env bash or #!/bin/bash, which tells the kernel which interpreter to invoke.

Q

What command makes a script file executable?

chmod +x script.sh (or chmod 755 script.sh).