BashLinuxmacOSWSLIntermediate~5h

Bash Scripting

Write shell that survives a filename

A script is not a saved command line — it is a small program in a language with its own rules about how many things a command receives. This is those rules: quoting, conditions that are really commands, loops with no list, functions that cannot return a value, and the three options everyone pastes at the top.

Modules
5
Drills
2
Build steps
11
Time
~5h

Walk out able to

Write a script from a specification that runs somebody else's command, survives its failures, and removes its own mess whichever way it ends.

What you work on

01

Quoting and arguments

How many things did the command actually receive?

02

Conditions and loops

A condition is a command; a loop has no list of its own.

03

Functions, status, traps

Hand a value back, report honestly, tidy up regardless.

04

Strict mode, honestly

What each option refuses, and where it stays silent.

The pitch

What you practise, and what you leave with

A retry wrapper: runs any command until it succeeds, keeps a log, cleans up on every path, and reports a status you can trust.

You will practise

  • Quoting, splitting and the argument list
  • Conditions, loops and spot-the-broken-line
  • Functions, exit status and traps
  • Each strict-mode option, and its gaps

Afterwards you can

  • Say what a command actually receives
  • Write a condition that means what it says
  • Report a status other programs can act on
  • Name where the abort option stays silent

Modules

5 modules, 34 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    Turn a command line into a file

    ~33 min

    Write a script the machine will run, and take arguments from whoever runs it.

    • LessonWhy put commands in a file4 min
    • Build stepWrite the file5 min
    • Build stepRun it without naming bash6 min
    • Build stepCount what it was handed5 min
    • LessonTake what the caller hands you13 min
  2. 2

    Quoting decides everything

    ~51 min

    Say exactly how many things a command receives, and stop losing the ones with spaces.

    • LessonWhat the command actually receives8 min
    • LessonWhy nothing warns you about it12 min
    • DrillCount what the command receives14 min
    • Build stepMake three files, two with spaces4 min
    • Build stepBreak it with one filename8 min
    • Build stepRepair it with two characters5 min
  3. 3

    Decide and repeat

    ~1h

    Ask a question the shell can answer, and loop over things rather than over text.

    • LessonAsk a question the shell can answer7 min
    • LessonChoose the right kind of equal8 min
    • LessonUse any command as a condition12 min
    • LessonThe list is not the loop's7 min
    • LessonFour loop forms, four counts12 min
    • LessonThe pattern that matched nothing8 min
    • DrillFind the line bash will not take9 min
  4. 4

    Hand back, report, clean up

    ~1h

    Write a function that returns something usable, and make the script tidy up after itself.

    • LessonWhat return actually returns7 min
    • LessonTwo channels out of a function8 min
    • LessonWhat local shields, and what it does not12 min
    • LessonWho actually reads a script7 min
    • LessonThe four rules of exit status12 min
    • LessonThere is no other cleanup7 min
    • LessonFour facts about traps12 min
    • Build stepWatch a handler run last6 min
    • Build stepWatch it clean up after a failure6 min
  5. 5

    Make it strict, then ship it

    ~1h

    Say what each of the three options refuses, then write a script that needs all of them.

    • LessonThree options, three jobs7 min
    • LessonName what each option refuses12 min
    • LessonWhy the exemption list exists7 min
    • LessonFive positions, and one consequence12 min
    • Build stepBuild something that fails at first6 min
    • Build stepWrite a retry wrapper from a spec12 min
    • Build stepRun it, and read the log8 min

The line you keep is a different thing from the line you type.

The first item is free. ~5h of focused work, at your own pace.

Intermediate~5h5 modules