Programming fundamentals · Beginner · 18 min
Use the terminal as a programmer: paths, arguments, exit codes and pipes
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GNU Bash 5.3 · GNU grep 3.12
You will move around the file system with paths, run a script with arguments, read exit codes, redirect output and finish with a one-line pipeline that finds the page failing most often in a log file.
On this page
If you have only used a terminal to paste install commands, it can feel like a box that either works or prints something frightening. It is more predictable than that. Every command you type is a program, or a command built into the shell, that receives a list of words, reads input, writes output and finishes with a number that says whether it succeeded. Once you can see those four things, you can combine small programs to answer real questions without writing a full program.
The program reading what you type is called the shell. On Ubuntu it is Bash; on macOS it is zsh. Every command in this tutorial works the same in both. You work in a throwaway folder, ~/terminal-demo, create a small web server log in it, and finish with a single line that answers: which page failed most often?
On Windows, the easiest way to follow along is WSL (Windows Subsystem for Linux), which gives you an Ubuntu terminal. If you stay in PowerShell, these are the closest equivalents to what you will use:
pwd,lsandcdwork as written; they are aliases forGet-Location,Get-ChildItemandSet-Location.echo $?becomes$LASTEXITCODEfor the exit code of a program. In PowerShell,$?is onlyTrueorFalse.echo $PATHbecomes$env:PATH, and folders are separated by;instead of:.export NAME=valuebecomes$env:NAME = "value".>and>>redirect output the same way.grep 'text' filebecomesSelect-String 'text' file.sort,uniq -candwc -lbecomeSort-Object,Group-ObjectandMeasure-Object -Line.
If your only computer is an Android phone, the Termux app gives you a Bash terminal where nearly all of this works. Your home folder there has a longer path than the ones shown below, which is a good reminder that pwd tells the truth about your machine, not about the tutorial.
Step 1 · Find out where you are
The terminal always has a current folder, called the working directory. Commands that take a file name look for it there unless you say otherwise. Ask where you are, then list what is in that folder:
cd ~
pwd
ls
ls -acd ~moves you to your home folder.~is shorthand for it.pwd(print working directory) prints the full path of the folder you are in.ls(list) prints the names of the files and folders in it.ls -alists all entries, including hidden ones whose names start with a dot, such as.bashrc. The-ais an option: a word starting with-that changes how a program behaves.
Check it worked
pwd prints a path that starts with /, something like /home/amara on Linux or /Users/amara on macOS. ls -a shows at least . and .. at the top, plus files that plain ls did not show. If your home folder is new, plain ls may print nothing at all, which is fine.
Step 2 · Move between folders with paths
A path is directions to a file or folder. There are two kinds:
- An absolute path starts with
/, the top of the file system, and works from anywhere./usr/binalways means the same folder. - A relative path does not start with
/and is read from the folder you are in.logs/app.logmeans "thelogsfolder here, thenapp.loginside it".
Two special names appear in every folder: . means this folder and .. means the folder above. Try them:
cd /usr/bin
pwd
cd ..
pwd
cd ~
pwd
cd -cd /usr/binuses an absolute path, so it works whatever folder you were in./usr/binholds many of the programs you run.cd ..goes up one level, to/usr.cd ~goes home again.cd -jumps back to the previous folder and prints its path. It is handy when you are switching between two places.
Check it worked
The four lines of output are /usr/bin, /usr, your home folder, and then /usr again from cd -. If you see No such file or directory, check the spelling and capital letters: /usr/Bin is a different path from /usr/bin on Linux.
Step 3 · Make a throwaway project folder
Create the folder for this tutorial, with a logs folder inside it, and move into it:
mkdir -p ~/terminal-demo/logs
cd ~/terminal-demo
pwd
lsmkdir makes a folder. With -p it also makes any missing parent folders and does not complain if the folder exists, so running the line twice is safe.
Now create a small web server log. Each line has a time, an HTTP status code, a method and the page requested. Status 200 means the request worked, 404 means the page was not found and 500 means the server failed:
cat > logs/app.log <<'EOF'
2026-11-18T09:00:01Z 200 GET /
2026-11-18T09:00:04Z 200 GET /products
2026-11-18T09:01:10Z 500 POST /checkout
2026-11-18T09:01:12Z 404 GET /favicon.ico
2026-11-18T09:02:30Z 200 GET /products/42
2026-11-18T09:03:05Z 500 POST /checkout
2026-11-18T09:03:40Z 500 GET /api/stock
2026-11-18T09:04:15Z 200 POST /login
2026-11-18T09:05:00Z 500 POST /checkout
2026-11-18T09:05:22Z 404 GET /old-page
2026-11-18T09:06:01Z 200 GET /
2026-11-18T09:06:45Z 500 GET /api/stock
EOF
ls -l logsThe <<'EOF' part is a here-document: the shell feeds every line up to the closing EOF into cat, and > (explained in step 7) writes what cat prints into logs/app.log. It is a way to create a file from the terminal without opening an editor. ls -l lists the folder in long form, with permissions, owner, size and date.
Check it worked
pwd prints your home folder followed by /terminal-demo. ls -l logs shows one file, app.log, with a size of a few hundred bytes. Run cat logs/app.log to see the twelve lines.
Step 4 · Run a script with arguments
When you type ls -l logs, the shell splits the line into words at the spaces. The first word is the program; the rest are its arguments. Write a tiny script that shows you exactly which arguments it receives:
cd ~/terminal-demo
cat > args.sh <<'EOF'
#!/usr/bin/env bash
echo "You gave $# arguments."
for arg in "$@"; do
echo "- $arg"
done
EOF
chmod +x args.sh
./args.sh one two "three four"Line by line:
#!/usr/bin/env bashis the shebang. It tells the system to run this file with Bash, found through yourPATH(step 6).$#is the number of arguments the script received."$@"is every argument, each kept as its own word. Theforloop prints them one per line.chmod +x args.shmarks the file as executable. Without it,./args.shfails withPermission denied../args.shruns the file in this folder. The./is a relative path meaning "here", which is needed for a reason you will see in step 6.
Check it worked
The output is You gave 3 arguments. followed by - one, - two and - three four. The quotes made three four a single argument. Run ./args.sh one two three four without the quotes and the count becomes 4. File names with spaces behave the same way, which is why programmers quote them.
Step 5 · Read exit codes with $?
Every program finishes with an exit code, a number from 0 to 255. Zero means success; anything else means some kind of failure. The shell keeps the last exit code in $?. Scripts, build tools and CI pipelines all decide what to do next by reading it.
grep searches for text and has well-defined exit codes: 0 if it found a match, 1 if it found none and 2 if something went wrong. Search the log for a status code that is there:
cd ~/terminal-demo
grep "404" logs/app.log
echo "exit code: $?"Now search for something that is not there. || means "run the next command only if the previous one failed", so the second half runs and reports the code:
grep "teapot" logs/app.log || echo "exit code: $?"The opposite, &&, runs the next command only if the previous one succeeded:
grep -q "500" logs/app.log && echo "the log contains server errors"The -q (quiet) option tells grep to print nothing and only set its exit code, which is what you want when the answer is yes or no.
In your own terminal you can also run a command, press Enter, and then type echo $? on its own line. Do it straight away: the next command you run replaces the value.
Check it worked
The first block prints the two 404 lines and then exit code: 0. The second prints nothing from grep, then exit code: 1. The third prints the log contains server errors.
Step 6 · Understand PATH and "command not found"
When you type grep, the shell has to find a program with that name. It does not search the whole disk. It looks through the folders listed in the PATH environment variable, in order, and runs the first match. Look at yours:
echo "$PATH"
command -v grep
type cdecho "$PATH"prints folders separated by:, such as/usr/local/bin:/usr/bin:/bin. The$in front of a name means "the value of this variable".command -v grepprints where the shell foundgrep, something like/usr/bin/grep.type cdsayscd is a shell builtin. Some commands are part of the shell itself, so there is no file to find.
Your project folder is not in PATH, which is why you typed ./args.sh. Try it without the ./:
cd ~/terminal-demo
args.sh hello || echo "exit code: $?"The shell prints something like args.sh: command not found and the exit code is 127, the shell's code for "no such command". You see the same error when a program is not installed, when you misspell its name, or when an installer put it in a folder that is not in PATH yet. That last one is common after installing developer tools, and the fix is to add the folder to PATH, then open a new terminal.
Add the project folder to PATH for this terminal session only, and the bare name works:
export PATH="$HOME/terminal-demo:$PATH"
args.sh helloPATH is one environment variable among many. Any variable you create lives in the shell; export passes it to the programs you start from that shell. See the difference with a variable of your own:
GREETING="Sannu"
bash -c 'echo "child sees: [$GREETING]"'
export GREETING
bash -c 'echo "child sees: [$GREETING]"'bash -c '...' starts a new Bash program that runs the quoted text. The single quotes stop your shell from replacing $GREETING itself, so the child has to look it up.
Check it worked
args.sh hello || ... prints a "command not found" message and exit code: 127. After the export PATH=... line, args.sh hello prints You gave 1 arguments. and - hello. The GREETING block prints child sees: [] first, because the variable was not exported, then child sees: [Sannu]. Both changes disappear when you close the terminal.
Step 7 · Send output to a file with redirection
Programs write normal output to a channel called standard output and error messages to a separate channel called standard error. Both appear in your terminal by default. Redirection sends them somewhere else.
> replaces the contents of the file it writes to, without asking. Point it at a new file name, never at a file you want to keep:
cd ~/terminal-demo
grep " 500 " logs/app.log > errors.txt
cat errors.txt>> adds to the end of a file instead of replacing it:
echo "checked the log" > notes.txt
echo "found 5 server errors" >> notes.txt
cat notes.txt2> redirects standard error. Ask grep to read a file that does not exist and send its complaint to a file:
grep "500" logs/missing.log 2> grep-errors.txt || echo "exit code: $?"
cat grep-errors.txtCheck it worked
cat errors.txt shows the five lines with status 500. cat notes.txt shows both lines, in order. The last block prints exit code: 2, grep's code for an error, and nothing else on screen; cat grep-errors.txt then shows the message, something like grep: logs/missing.log: No such file or directory.
Step 8 · Pipe programs together to answer a question
A pipe, written |, connects the standard output of one program to the standard input of the next. Each program does one small job; the pipe passes the result along without a file in between. Build the answer one stage at a time, running each line and looking at what it prints:
cd ~/terminal-demo
grep " 500 " logs/app.log | wc -l
grep " 500 " logs/app.log | cut -d " " -f 4
grep " 500 " logs/app.log | cut -d " " -f 4 | sort
grep " 500 " logs/app.log | cut -d " " -f 4 | sort | uniq -cgrep " 500 " logs/app.logkeeps only the lines with status 500. The spaces around500stop it matching a time such as09:05:00.wc -lcounts lines, so the first command answers "how many server errors?"cut -d " " -f 4splits each line at spaces (-dsets the delimiter) and keeps the fourth field, the page.sortputs identical pages next to each other.uniq -ccollapses each run of identical lines into one and puts the count in front. It only merges neighbouring lines, which is whysortcomes first.
Now add the last two stages: sort by that count, largest first, and keep the top line. This is the one-line answer to "which page failed most often?":
grep " 500 " logs/app.log | cut -d " " -f 4 | sort | uniq -c | sort -rn | head -n 1sort -rn sorts numerically (-n) in reverse (-r), and head -n 1 keeps only the first line. Swap " 500 " for " 404 " and the same line tells you which missing page people request most. On a real server the log might have a million lines; the pipeline is the same and still finishes in seconds.
Check it worked
The first command prints 5. The uniq -c line prints two counts, 2 /api/stock and 3 /checkout, with padding before each number. The final pipeline prints one line, something like 3 /checkout: the checkout page failed three times.
Step 9 · Stop a program, end input and clean up
Two key combinations look similar and do different things. Try each in your terminal; these are interactive, so type them yourself.
Start a program that waits for a minute, then press Ctrl+C:
sleep 60Ctrl+C interrupts the running program and gives you the prompt back straight away. Use it whenever something hangs or you started the wrong command.
Now start cat with no file name. It reads what you type instead. Type a line, press Enter, then press Ctrl+D on an empty line:
catcat repeats each line back to you. Ctrl+D does not kill anything: it tells the program "there is no more input", so cat finishes normally. Pressed at an empty prompt, Ctrl+D ends the shell itself and closes the terminal or logs you out of a server, so use it with care.
Last, remove the throwaway folder. rm -r deletes a folder and everything inside it, with no recycle bin and no undo. Run pwd first and type the path out in full rather than using a wildcard:
cd ~
pwd
rm -r ~/terminal-demo
ls ~/terminal-demo || echo "exit code: $?"Check it worked
After Ctrl+C, the prompt returns before the minute is up. After Ctrl+D, cat stops and the prompt returns. After rm -r, the last command prints a "No such file or directory" message and a non-zero exit code: the folder is gone. The PATH change from step 6 points at a folder that no longer exists, which is harmless; it disappears when you close the terminal.
Next steps
You can now read a path, pass arguments, check exit codes, redirect output and chain programs with pipes. These ideas run through everything else you will do in a terminal.
- Hello World in Bash turns these commands into a script with its own exit codes and strict mode.
- Linux file permissions with chmod and chown explains what the letters in
ls -lmean and whatchmod +xchanged. - Process JSON on the command line with jq adds a tool for the structured data many APIs and logs use, in the same pipeline style.
- What happens when you run a program looks at what the shell actually starts when you type a command.
Related tutorials
- Write your first class in Python: objects, attributes and methodsYou will model a library book loan as a Python class with attributes, methods, a readable __repr__ and a shared loan period, compare it with a dictionary version, rewrite it as a dataclass and end with a Loan class and a short script that uses it.
- Write your first JavaScript tests with the built-in Node.js test runnerYou will write a small price module in JavaScript, cover it with six passing tests using node:test and node:assert, and know how to read a failure and run one test on its own, with no packages installed.
- Write readable code: names, comments and a formatterYou will turn a working but cryptic Python script into a readable one with clear names, named constants, small functions and useful comments, tidy it with Ruff, prove its output has not changed and leave with your own style checklist.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials