How to teach shell scripting to a beginner

Start with what the shell actually does

Before syntax, teach the beginner to see the shell as a prompt that accepts one instruction at a time. Sit beside them for the first 10 minutes and use one terminal window only. Ask them to type pwd, ls and echo hello, then say what each line will do before pressing Enter. If they can predict the output of three short commands, they have the mental model they need for a first script. Do not introduce variables, loops or conditionals in this session. Keep a notepad visible with four labels: prompt, command, output, next prompt. End the session by saving one command in a file and running it, so the move from typing to scripting feels small.

Set a pace that suits a complete beginner

Plan 45–60 minute lessons, two or three times a week, with a 10-minute break between demonstration and independent practice. Aim for 80% hands-on time and no more than 5 minutes of uninterrupted explanation. One new idea per session is enough. If the learner cannot type a five-line script without copying you by the end of lesson three, repeat the same exercise with different text rather than adding a new command. Add a 20-minute practice block two days after each lesson. Shell scripting is free to practise, so the limit is attention, not software cost.

Teach the four-part script skeleton first

Give every beginner script the same four parts: a shebang line, a short comment saying what the script does, the commands, and a clear final output or exit message. Keep the first script to five lines or fewer. Explain that the shebang tells the operating system which shell to use, while comments are for humans and are ignored when the script runs. Show the difference between typing a command at the prompt and saving it in a file, then explain why the file needs execute permission. Use one small task, such as printing today’s date and the current user, and ask the learner to change the wording themselves. The goal is not a useful tool yet; it is a shape they can recognise every time.

Use a repeatable lesson format

A 50-minute lesson can follow this structure: 5 minutes reviewing the previous script, 10 minutes watching you demonstrate one change, 20 minutes of guided typing where the learner drives, 10 minutes of independent work with the screen turned away from you, and 5 minutes explaining what went wrong. Write the lesson goal in one sentence at the top of the page. Finish with a two-minute summary from the learner, not from you. Keep a one-page error log: the command, the message and the fix. This format gives beginners repetition without boredom and makes progress visible across four to six sessions.

Move from one command to a guarded script in small steps

Start with a single command in a file. Next, add one argument, then one check that the argument exists, then one readable error message. Introduce no more than two new syntax items per lesson. If a script grows past 10–15 lines during teaching, split it into two scripts or remove the least important branch. Use a fixed input file and a fixed expected output so the learner can compare results. Have them run the script three times: with correct input, with missing input and with deliberately wrong input. That third run is where the learning happens. It teaches them to expect failure and to read the error instead of guessing.

Make the beginner explain the script back to you

After each working script, ask for a two-minute line-by-line explanation in plain English. Stop at any line they cannot explain and mark it for the next lesson. A useful threshold is 80% correct explanation before moving on: they may forget a flag, but they must know what the line is trying to do. Ask prediction questions such as “What will print if the folder is empty?” and “Which line runs first?” This exposes fragile copying. If they can explain it, they can debug it; if they cannot, more examples will not help yet. Ask them to rewrite one line in a different but equivalent way before you move on.

Diagnose the common “it works when I type it, but not in the script” failure

Symptoms: the command succeeds at the prompt, but the saved script reports “command not found”, prints the wrong folder or does nothing. Likely causes: the script is being run from a different working directory; quotes are missing around a path with spaces; the file lacks execute permission or a correct shebang line; the script was saved with Windows line endings; or a variable is empty because it was never set. The fix: run the script from the same folder as the files it uses, print the working directory at the top, quote every path, make the file executable, and trace the script with the shell’s debug mode. Ask the learner to add one temporary print statement before the failing line. Remove it only after the cause is understood. Do not simply rewrite the script for them; make them change one cause at a time and rerun.

Teach safety and independence together

Before any lesson involving deleting, moving or overwriting files, spend 10 minutes on a test folder and a dry run. Teach the beginner to read a command aloud before pressing Enter, to avoid wildcards in destructive commands until they can explain what will match, and to keep backups of anything they cannot recreate. Set a simple rule: no administrative privileges for beginner exercises, and no running scripts copied from strangers. Show them how to ask the shell for help and how to stop a running script safely. Independence means they can recognise danger, not that they never make mistakes. A learner who can write a 10–15 line script, explain each line and recover from one error is ready to practise alone with review.

FAQ

How long should each teaching session last?

Use 45–60 minutes for beginners, with at least 20 minutes of typing and 5 minutes of recap. Shorter, frequent sessions beat one long weekly lesson.

What should I do if the beginner freezes at the prompt?

Return to three commands they already know, ask them to predict the output, and let them type only one line before you speak again. Reduce the task to a five-line script with one change.

Should I teach Bash or another shell first?

Teach one shell consistently for the first four to six sessions. A common choice is Bash because many examples and beginner tutorials use it, but the teaching method matters more than the shell name.