How to read a shell scripting pattern or diagram

A shell scripting pattern is a repeated shape in code: a test, a loop, a pipeline. A diagram is a picture of that shape: boxes, arrows, and labels. Read both the same way: find the start, name each symbol, then follow one path at a time.

Find the legend and the flow direction first

Spend 30–60 seconds on the key, caption, or labels before reading a command. A rectangle usually means run this command; a diamond means test this condition; a rounded box marks start or end; a backward arrow marks a loop. If there is no legend, use the labels: if, while, for, then, and done are strong clues. Check whether arrows show control flow (what runs next) or data flow (what output feeds what input). They are not the same. In a pipeline diagram, an arrow from grep to sort usually means stdout becomes stdin, not just “then”. Look for arrow labels such as stdout, stdin, exit 0, and exit 1. If labels are missing, read the punctuation: | is a pipe, ; is a sequence, && is a conditional sequence.

Separate control flow from data flow

Control flow answers which line runs next; data flow answers where the bytes go. In grep "error" app.log | sort | uniq -c, the boxes are commands and the arrows are a pipe: stdout of one command becomes stdin of the next. That is one data path, not three unrelated steps. Compare cd /tmp; ls, where ls always runs, with cd /tmp && ls, where ls runs only if cd returns exit code 0. The shell convention is 0 for success and 1–255 for failure. Check 2> too: it sends stderr to a file, while > overwrites stdout and >> appends. If a diagram shows one output arrow, ask whether stderr is hidden.

Read the skeleton before the commands

Do a 15-second skeleton pass: find the start, the end, every diamond, and every backward arrow. One diamond with two forward branches is probably an if/else; one diamond with a backward arrow is probably a while or until loop. Mark the shape on paper as Start -> input -> test -> action -> end. Read commands only after that. In a script, line 1 is often a shebang such as #!/bin/sh or #!/bin/bash; it names the intended interpreter. The first 10 lines often show arguments and defaults. The last 10 often show output, cleanup, or an explicit exit. Read those boundary lines before the middle.

Decode the common symbols and labels

Keep a small glossary. Rectangle: command. Diamond: condition. Parallelogram: input or output. Rounded box: start, end, or connector. $1 is the first argument, $# is the argument count, and $? is the previous exit status. A test such as [ -f "$1" ] asks “is argument one an existing regular file?” and produces a yes/no answer, which is why it belongs in a diamond. A placeholder such as <input-file> is a slot, not literal text. Dots or a dotted box hide repeated steps; find the loop arrow that explains them. Indentation of 2 or 4 spaces is for human eyes, not shell syntax, but it shows which commands sit inside a block. An arrow labelled exit 1 leaves on failure, not success.

Trace one path at a time

Put a finger on the start and follow one route to the end. At each diamond, read the condition exactly and choose yes or no. At each loop, write down the state: variable name, current value, and what changes on the next pass. A 100–200 line script can be traced in 5–10 minutes this way. If the box says while [ "$COUNT" -lt 3 ], look for the line that increases COUNT. If nothing changes it, the backward arrow warns that the loop may never end. Trace the failure branch too. A tiny pattern to try: start -> [ -d "$1" ] -> yes: echo directory; no: echo not a directory -> end. The diamond tests whether argument one is a directory; the two forward arrows are different paths.

Check the boundaries and assumptions

A pattern is readable only when you know what enters and leaves it. Find the input source: a filename, a pipe, an argument, or a prompt. Find the output destination: screen, file, another command, or nothing. Check the working directory: logs/app.log is relative, while /var/log/app.log is absolute. Check permissions: reading needs read permission; running as a program needs execute permission. Check the interpreter: bash-only features such as arrays or [[ ]] may not work under #!/bin/sh. If the diagram has no error branch, ask where failures go. If it has no start or end, infer them from the shebang and the last command or exit.

Common failure: you follow the arrows but the script does something else

Symptoms: your traced path never reaches a block that clearly runs; a loop looks finite on paper but runs forever; output order differs from the diagram; or an error appears on screen although the diagram sends output to a file. Likely causes: you read a pipe as a sequence; you missed that && skips the next command after a non-zero exit; you read a diamond’s yes/no arrows backwards; you ignored stderr; or you missed a cd that changes which file is read. The fix: redraw that section with two labels on each arrow, data or control and success or failure. Run bash -x script.sh; it prints each command as it runs, prefixed with +. Read 20–30 lines of the trace beside the diagram. If they disagree, trust the trace and correct your reading of the symbols.

When the diagram is incomplete or wrong

Some diagrams omit the legend, merge stdout and stderr, or draw a loop without an update step. Do not force a meaning. Look for a caption, file name, or nearby text that names the shell and the purpose. Compare the diagram with the smallest runnable snippet, usually 3–10 lines. Mark uncertain arrows with a question mark and test one assumption at a time. If a box says “process file” but never names the file, treat it as a placeholder. If two arrows leave a diamond with no labels, the upper or left arrow is often “yes” by convention, but confirm it from the text. A diagram you cannot trace from start to end in one path is not yet readable; redraw it with a clear start, end, and one label per arrow.

FAQ

What if the diagram has no legend?

Use labels as the legend. Words such as if, while, for, then, do, and done identify control shapes; |, ;, &&, and 2> identify data and sequencing. If an arrow is still unclear, test a 3–10 line snippet.

Should I read a shell script top to bottom or follow the arrows?

Do both. A 15-second top-to-bottom pass finds start, end, decisions, and loops. Then trace one path at a time with the arrows. Top-to-bottom reading alone hides branches; arrow-following alone can lose the overall structure.

How can I tell a loop from a repeated command in a diagram?

Look for a backward arrow or a done label that returns to an earlier test. Two separate boxes with no backward arrow are probably two steps, not a loop. Check whether a variable changes between passes; if nothing changes, the backward arrow is suspicious.