Linux, terminal and Git

Stage 7 · Lesson 5 of 8

Functions and readable scripts

Shell scripts and automation

group steps under a name and make a script easier to read. Write backup_db() { ... }, then call it with backup_db. Inside a function, declare variables with local so they do not leak into the rest of the script.

A clean script structure has a usage message, checks, small named functions and a main function that is called at the bottom. Print progress messages to and real results to standard output, so the script still works inside a pipeline.

End with a clear , so callers can tell whether it worked. Add a comment for anything surprising. Once a script passes about 100 lines or needs complex data, is usually clearer.

Common trapa 300-line script with no functions that only its author, on a good day, understands.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Paste or run a script to catch quoting and error-handling bugs before they bite

  • Check what each part of an unfamiliar command does before you put it in a script

Also worth your time

Mark this stage as not done?