Back to cert prep

Practice area

LPIC-1 102: Shells and Shell Scripting

Control shell environment behavior and write small scripts that are predictable, readable, and safe to run repeatedly.

Linux One Liners is an independent study and practice resource. It is not affiliated with, endorsed by, or approved by LPI, The Linux Foundation, CompTIA, or any certification provider. This site does not provide exam dumps or real exam questions.

Source status

Source status: LPI LPIC-1 overview verified July 3, 2026. Current version 5.0; exams 101-500 and 102-500.

This page paraphrases study areas into command practice. It does not copy official objective text wholesale and is not an exam dump.

Plain-English goal

Practice area: shell environment, startup files, aliases, functions, scripts, tests, loops, and exit codes. Exam/domain: 102-500.

read the situation

Command, output, and next step

Command anatomy

printf 'SHELL=%s\nPATH=%s\n' "$SHELL" "$PATH"; env | sort | head -40
printf
the command family
flags
change output shape or scope
target
the file, service, user, mount, or host being inspected
output
evidence you must explain before changing state

Annotated output

Usage: command [OPTION]... TARGET
Try 'command --help' for common flags.
Try 'man command' for full reference.

What to notice

Usage
the command shape and expected target
--help
quick option reference
man
full local manual page when installed

Safe vs unsafe move

Common wrong move

Treating a practice command as a permission to make a broad production change.

Next safe command

command --help

Troubleshooting ladder

  1. Name the symptom.
  2. Inspect read-only state.
  3. Find the owner, service, file, device, mount, or route.
  4. Read the decisive output field.
  5. Choose the next narrow command.
  6. Avoid broad or destructive changes.
  7. Make the smallest justified change if required.
  8. Verify and record what changed.

How to get help

  1. Know the commandUse command --help, then man command for the full reference.
  2. Know the conceptUse apropos keyword or man -k keyword to discover command names.
  3. Maybe a shell builtinUse type command, command -V command, then help command.
  4. Service behaviorUse systemctl status service and journalctl -u service before restarting.
  5. Package ownershipUse dpkg -S, rpm -qf, or the distro package tool for the installed file.

Study plan

  1. Separate login shell, interactive shell, non-interactive script, and cron execution so environment surprises are easier to debug.
  2. Practice PATH, aliases, functions, variables, quoting, and command substitution with visible output before relying on them.
  3. Write small scripts with a shebang, input checks, exit-code checks, and clear output for success/failure.
  4. Connect scripts to scheduled jobs carefully: full paths, logging, predictable locale, and no hidden interactive assumptions.

Command labs

Run these in a lab shell or disposable machine first. The point is to explain the output, not just memorize the command.

Inspect shell environment

printf 'SHELL=%s\nPATH=%s\n' "$SHELL" "$PATH"; env | sort | head -40

Shell path and environment variables should be visible and explainable.

Annotated output
Usage: command [OPTION]... TARGET
Try 'command --help' for common flags.
Try 'man command' for full reference.

What to notice: Usage, --help, man.

Next safe command: command --help

Check cron path assumptions

crontab -l 2>/dev/null; grep -R '^PATH=' /etc/cron* 2>/dev/null | head

User and system cron environment clues should show whether scripts rely on missing paths.

Annotated output
Usage: command [OPTION]... TARGET
Try 'command --help' for common flags.
Try 'man command' for full reference.

What to notice: Usage, --help, man.

Next safe command: command --help

Test script exit behavior

sh -n ./script.sh 2>/dev/null; printf 'syntax_check_exit=%s\n' "$?"

Shell syntax check should return 0 for parseable scripts and nonzero for syntax errors.

Annotated output
Usage: command [OPTION]... TARGET
Try 'command --help' for common flags.
Try 'man command' for full reference.

What to notice: Usage, --help, man.

Next safe command: command --help

command families

Commands to practice

  • bash
  • sh
  • env
  • export
  • alias
  • test
  • [
  • for
  • while
  • case
  • read
  • set
  • crontab

Related drills

Flashcards

Why do cron jobs fail when manual commands work?

Cron often has a smaller environment and PATH than an interactive shell.

What does sh -n do?

It checks shell syntax without executing the script body.

Why quote variables in shell scripts?

Quoting prevents word splitting and glob expansion surprises.

What does an exit code of 0 conventionally mean?

Success; nonzero usually means failure or a special condition.

Quick quiz

Check the reasoning locally in your browser. Answers are not sent anywhere.

A cron job cannot find a command. What should you inspect first?
Show answer

Answer: PATH and full command paths

Why: Cron PATH is often minimal.

  • GPU temperature: That does not answer the question the output is asking you to prove first.
  • printer queue: That does not answer the question the output is asking you to prove first.
  • DNS CAA: That does not answer the question the output is asking you to prove first.
Which line chooses the shell interpreter for a script?
Show answer

Answer: shebang

Why: A shebang such as #!/bin/sh or #!/usr/bin/env bash selects the interpreter.

  • umask: That does not answer the question the output is asking you to prove first.
  • fstab: That does not answer the question the output is asking you to prove first.
  • resolv.conf: That does not answer the question the output is asking you to prove first.
Which practice prevents syntax errors before execution?
Show answer

Answer: sh -n script.sh

Why: sh -n parses without running commands.

  • rm script.sh: That destroys or removes state before the evidence is understood.
  • chmod 777 /: That changes access broadly before proving which path component or owner is wrong.
  • kill -9 $$: That changes runtime state before reading the output that explains the failure.

Self-test before moving on