Stage 7 · Lesson 5 of 8
Functions and readable scripts
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
- GuideFreeGreg's Wiki: BashGuide
Greg's Wiki (Wooledge) · Beginner
A careful, accurate Bash guide that teaches the habits that avoid most scripting bugs
- CourseFreeThe Missing Semester of Your CS Education
MIT · Beginner
The shell tools and scripting lecture, with exercises that build useful small scripts
Practise
Keep handy
Also worth your time
- roadmap.sh: Shell / BashGo deeper
The scripting side of the shell map: arrays, traps, parameter expansion and more