How to Learn Shell Scripting: the questions every beginner asks

What is a shell script, in one sentence?

A shell script is a plain-text file containing commands that a shell reads and runs in order. It is not compiled: you write backup.sh, make it executable with chmod +x backup.sh, and run ./backup.sh. The first line is often #!/usr/bin/env bash, which tells the system to use Bash. A script can be 5 lines or 500; the useful beginner size is 5-20 lines. It returns an exit code: 0 means success, and anything from 1 to 255 means some kind of failure. That number matters more than colourful output.

Do I need to learn programming before shell scripting?

No. You need a handful of terminal habits: moving between folders, reading a command's output, and understanding that a command either succeeds or fails. The programming ideas you will use first are variables, if, for, and exit codes — four ideas, not a full computer-science course. If you can spend 20-30 minutes, three times a week, you can start writing small scripts in the first week. You do not need to know Python, C, or algorithms first. You do need to be willing to read error messages slowly and test one line at a time.

Which shell should a beginner learn first?

Learn Bash first unless your job already uses another shell. Bash is common on Linux, available on macOS, and supported inside Windows through WSL or Git Bash. Version matters: macOS may ship Bash 3.2, while Linux systems often have Bash 5.x. Write for Bash 3.2-compatible syntax at first, and start scripts with #!/usr/bin/env bash so the file is explicit. Zsh and Fish are fine interactive shells, but their scripting differences will slow a beginner who is trying to read ordinary examples. Learn one shell to the point of comfort — about 2-4 weeks of regular use — before collecting others.

How long does it take to learn the basics?

For a complete beginner, expect 4-6 weeks to become comfortable with 5-10 line scripts if you practise 20-30 minutes on 3-4 days each week. Expect 8-12 weeks before you can write a 30-50 line script that takes arguments, checks files, and reports a clear error. These ranges assume you write and run code, not just read it. A realistic first month is 3-5 tiny scripts: list files, count lines, rename a set of files in a copy folder, check whether a service responds, and print a dated message. Mastery is slower; basic usefulness is not.

What should I practise in the first two weeks?

Practise four things in tiny scripts rather than one large one:

First, print text and variables with echo, including a quoted variable such as "$name".

Second, accept one or two arguments and check them: run ./greet.sh Sam and fail clearly if $1 is empty.

Third, use if with a test, such as [ -f "$file" ], and print missing or found.

Fourth, loop over 3-5 files in a temporary folder and count them.

Keep each script under 20 lines. Run it, break it on purpose, and run it again. If you spend 30 minutes on one 5-line script, that is normal. The goal is not speed; it is being able to predict what the next line will do.

Why does my script fail when a file name contains a space?

Symptoms: a loop treats my file.txt as two names, rm reports "No such file", or a variable expands into several arguments. Likely cause: an unquoted variable or command substitution. In Bash, $file undergoes word splitting and filename expansion; "$file" does not. The fix is to quote every variable unless you have a deliberate reason not to: use cp -- "$file" "$backup_dir/", for file in "$dir"/*, and while IFS= read -r line. For positional parameters, use "$@", not $*. Add set -u so an unset variable stops the script instead of silently expanding to nothing. Test with a file called a b.txt before trusting the script with real data.

How do I know what a script is doing when it goes wrong?

Run bash -n script.sh first; it checks syntax without executing anything. Then run bash -x script.sh to print each command after expansion — the + lines show exactly what the shell tried to run. Check the exit code immediately after a command with echo $?: 0 is success; 1–255 is failure. For stricter scripts, start with set -euo pipefail, but understand it: -e stops on many failures, -u stops on unset variables, and pipefail catches failures inside pipelines. Use printf '%s\n' "$var" when a value looks wrong, because it shows hidden spaces and empty strings. Debug one line at a time; do not rewrite the whole script after the first error.

When am I ready to write a real script?

You are ready when you can read a 20-line script and explain what each line does, quote variables without being reminded, accept and validate 1-3 arguments, loop safely over file names with spaces, and check an exit code before continuing. A useful test: write a 30-50 line script that copies files into a dated folder, refuses to overwrite existing files, and prints a clear message on failure. If you can do that with bash -n and one test run, you are past the beginner stage. You do not need to know every command; you need to know how to find out what a command does before running it.

FAQ

Do I need to memorise commands?

No. Learn 10-15 commands well — pwd, ls, cd, cp, mv, rm, mkdir, cat, grep, wc, head, tail, chmod, echo, and test — and look up the rest. Use command --help or man command for exact flags. Memorising flags you never use is wasted effort.

Can I learn shell scripting on Windows?

Yes, but use a Bash environment: WSL is the closest to Linux, and Git Bash is a lighter option for small scripts. Plain Command Prompt and PowerShell use different syntax, so examples written for Bash will not run unchanged. Set aside 30-60 minutes for the first environment install, then test with a one-line script that prints hello. Keep paths simple at first; spaces and backslashes are common early stumbling blocks.

Should I learn Python instead?

Learn shell scripting first for file operations, running commands, and gluing programs together; learn Python when you need data structures, libraries, or a program larger than roughly 50-100 lines. Shell is excellent for a 20-line backup or rename script and awkward for complex parsing. Many beginners use both: a shell script to start the job, and Python for the part that needs real data handling. Do not delay your first script while deciding.