Objective. Students will identify and correctly use fundamental Bash constructs—variables, quoting rules, command substitution, pipelines, and redirections—as the building blocks of all subsequent DBA automation.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Database administrators spend a large portion of every workday at the terminal. Whether you are checking which PostgreSQL processes are consuming memory, rotating log files, or scheduling a nightly backup, the Linux shell is your control panel. This lesson builds the Bash foundation that every automation script you write later will depend on. Accuracy matters here: a misplaced quote or a forgotten redirect can corrupt data or overwrite a file you needed.
Bash (Bourne Again SHell) is the default interactive shell on most Linux distributions, including CentOS and Ubuntu. When you open a terminal, Bash reads your commands, interprets them, and passes instructions to the Linux kernel. Every DBA automation script in this course is a plain text file of Bash commands saved with a `.sh` extension and executed by the Bash interpreter.
Bash reads commands from left to right. Before executing anything, it performs a set of expansions in a defined order: brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion. Understanding this order prevents hard-to-find bugs.
A variable stores a value for later use. Assignment uses no spaces around the equals sign:
```bash
DB_HOST=localhost
DB_PORT=5432
```
Retrieve the value by prefixing the name with `$`:
```bash
echo $DB_HOST
echo "Connecting to ${DB_HOST}:${DB_PORT}"
```
Braces around the name (`${DB_HOST}`) are optional when the variable name is unambiguous, but they are considered best practice because they prevent accidental concatenation with adjacent characters. Variable names are case-sensitive; by convention, environment and configuration variables are written in ALL_CAPS, while loop counters use lowercase.
Quoting controls which characters Bash interprets and which it treats as literal text. There are three quoting mechanisms:
A common DBA mistake is forgetting to quote a variable that contains spaces, causing a filename or connection string to be split into multiple arguments. Always quote variables that may contain whitespace: `"$DB_NAME"`.
Command substitution runs a command and inserts its output inline. The preferred modern syntax uses `$(...)` rather than backticks, because it nests cleanly:
```bash
CURRENT_DATE=$(date +%Y-%m-%d)
echo "Backup started on $CURRENT_DATE"
```
DBAs use command substitution to capture dynamic values—row counts, process IDs, or timestamps—directly into variables without writing intermediate files.
A pipeline connects commands so the standard output (stdout) of one command becomes the standard input (stdin) of the next, using the pipe character `|`:
```bash
ps aux | grep postgres | grep -v grep
```
This lists all running processes (`ps aux`), filters for lines containing "postgres," and then removes the grep process itself from the results. Pipelines are fundamental to DBA work: they let you filter, sort, count, and transform data streams without writing temporary files.
Redirection changes where a command reads from or writes to:
| Operator | Meaning |
|----------|---------|
| `>` | Write stdout to a file, overwriting it |
| `>>` | Append stdout to a file |
| `<` | Read stdin from a file |
| `2>` | Redirect stderr (error output) to a file |
| `2>&1` | Merge stderr into stdout |
Example: saving a PostgreSQL query log and capturing errors separately:
```bash
psql -U postgres -c "SELECT count(*) FROM pg_stat_activity;" > query_out.txt 2> query_err.txt
```
Overwriting a file with `>` is irreversible on most filesystems. Before using `>`, confirm you do not need the existing content; use `>>` to append when in doubt.
The Linux command line operates with the permissions of the logged-in user. Running database administration tasks as the `root` user or the `postgres` superuser without need is risky: a typo in a destructive command has immediate effect. Follow the principle of least privilege—use a regular OS account and `sudo` only for tasks that require elevated permissions, and only for the specific command needed. Before running any command that modifies or deletes data, test it in a non-production environment. When writing scripts that will run as scheduled jobs, store credentials in environment variables or a secured configuration file, never hard-coded in the script itself.
Key navigation commands every DBA uses daily:
Process inspection:
Every DBA automation script—backup jobs, health checks, log rotation—is assembled from exactly these building blocks: variables that hold configuration, quoting that protects string values, command substitution that captures live data, pipelines that transform output, and redirections that preserve logs. Mastering these fundamentals now means every script you write in the remaining modules will be readable, reliable, and maintainable.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.