Technical skill

Bash programming language

Subject: .../Bash

Save command-line steps in a script

Bash is a shell: it reads commands, then starts programs to do the requested work. You can type a command at a prompt or save several commands in a script, a text file Bash runs in order. A script is useful when the same check or set of steps needs to be repeated. For example, this one accepts a file path and checks whether the file can be read.

Before looking at the code, two terms help. An argument is a value written after the command name, such as a filename. A variable gives a name to a value so the script can use it later. Quoting a variable keeps a path with spaces together as one argument.

#!/usr/bin/env bash
set -u

if (($# != 1)); then
    printf 'Usage: %s FILE\n' "$0" >&2
    exit 2
fi

file=$1
if [[ ! -r "$file" ]]; then
    printf 'Cannot read: %s\n' "$file" >&2
    exit 1
fi

The first line, called a shebang, selects Bash when the file is run directly. set -u makes Bash report an error when the script uses a variable that was never set. $# is the number of arguments; the first if requires exactly one. The script saves that argument in file, checks whether it is readable, prints messages to standard error, the separate stream used for error messages, and uses different nonzero exit statuses for bad usage and an unreadable file. [[ ... ]] is a Bash test expression.

How Bash turns text into arguments

Bash processes a command before starting it. It expands variables and command substitutions, replacing forms such as $(...) with the output of a command, then may split values into words or expand filename patterns, and finally sets up redirections. Double quotes allow a variable to expand while keeping its value in one argument. Single quotes keep their contents literal. Those rules matter when a filename contains spaces or characters Bash treats specially.

When a command uses values assembled from variables, a Bash array keeps each part separate:

args=(grep -n -- "$pattern" "$file")
"${args[@]}"

Each array element stays one argument, including a path with spaces. eval parses its input as shell code, which makes quoting and trust boundaries harder to control.

Functions give repeated operations a name. if, case, for, and while express choices and loops. Check command statuses where failure is expected. Bash's set -e behaves differently in conditionals, pipelines, and compound commands, so it does not replace checking which command can fail. set -o pipefail makes a pipeline report failure when one of its commands fails.

Test scripts with ordinary input, a path with spaces, and a command that fails. ShellCheck, a tool that checks shell scripts, can flag common quoting mistakes; review each warning in context. Use printf for formatted output, include clear usage text, make temporary-file cleanup explicit, and avoid assuming the caller's current folder unless that is part of the interface. Scheduled scripts have their own environment and working folder, so state those dependencies. If a script must work in different shells, use POSIX shell syntax, the standardized subset many shells support; for example, [[ ... ]] is Bash-specific.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful