When It Goes Wrong — Reading Errors Instead of Fearing Them
I have watched a lot of people stop right here. Not at pipes, not at scripts — at the first block of red text. It looks like a wall and it feels like being told off. It is neither. An error message is an address: it tells you where the problem is and usually what it is.
Read the last line first
This is the whole trick and it feels wrong the first few times. When a command prints twenty lines of error, the lines at the top are usually the route it took to get to the problem. The bottom line is the problem. Start there, then read upwards only if you need more.
Then look inside that line for two things: a filename and a number. The number is a line number. Together they are the exact spot. Most error messages are far more specific than they look, once you stop reading them as a paragraph and start reading them as a label.
Errors travel on a separate channel
This one confuses everybody once, and then never again. A command has two separate outputs: normal results, and errors. They both land on your screen, so they look like one stream — but they are not.
ls nosuchfile > out.txtThat is not a bug and nothing was lost. The error went down the other channel, which the plain arrow does not touch. To catch that one you put a 2 in front of the arrow:
ls nosuchfile 2> errors.txtThe most misleading error there is
command not found does not mean you typed it wrong, and it does not mean the program is missing. It means the shell looked in its list of folders and did not find it there. That list is called PATH.
echo $PATHwhich grepThese two turn a dead end into a question you can answer. If the program is installed but its folder is not in PATH, the shell genuinely cannot see it — the fix is the folder, not the command. And which is also how you check that the thing running is the thing you think it is, which matters on Windows where the same name can belong to a different program.
Every command leaves a receipt
echo $?Run it right after something and you get a number. Zero means success — which is backwards from what you expect, and worth fixing in your head now. Anything else is a failure, and the number often says which kind. Three I checked on this machine: a search that found something gave 0, the same search finding nothing gave 1, and a name that does not exist at all gave 127.
Errors have stopped being a wall. Next: everything you have typed so far disappeared when you closed the window. Time to keep it.