Skip to main content

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, ls and cd work as written; they are aliases for Get-Location, Get-ChildItem and Set-Location.
  • echo $? becomes $LASTEXITCODE for the exit code of a program. In PowerShell, $? is only True or False.
  • echo $PATH becomes $env:PATH, and folders are separated by ; instead of :.
  • export NAME=value becomes $env:NAME = "value".
  • > and >> redirect output the same way.
  • grep 'text' file becomes Select-String 'text' file.
  • sort, uniq -c and wc -l become Sort-Object, Group-Object and Measure-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:

Bash
cd ~
pwd
ls
ls -a
  • cd ~ 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 -a lists all entries, including hidden ones whose names start with a dot, such as .bashrc. The -a is 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/bin always means the same folder.
  • A relative path does not start with / and is read from the folder you are in. logs/app.log means "the logs folder here, then app.log inside it".

Two special names appear in every folder: . means this folder and .. means the folder above. Try them:

Bash
cd /usr/bin
pwd
cd ..
pwd
cd ~
pwd
cd -
  • cd /usr/bin uses an absolute path, so it works whatever folder you were in. /usr/bin holds 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:

Bash
mkdir -p ~/terminal-demo/logs
cd ~/terminal-demo
pwd
ls

mkdir 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:

Bash
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 logs

The <<'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:

Bash
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 bash is the shebang. It tells the system to run this file with Bash, found through your PATH (step 6).
  • $# is the number of arguments the script received.
  • "$@" is every argument, each kept as its own word. The for loop prints them one per line.
  • chmod +x args.sh marks the file as executable. Without it, ./args.sh fails with Permission denied.
  • ./args.sh runs 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:

Bash
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:

Bash
grep "teapot" logs/app.log || echo "exit code: $?"

The opposite, &&, runs the next command only if the previous one succeeded:

Bash
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:

Bash
echo "$PATH"
command -v grep
type cd
  • echo "$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 grep prints where the shell found grep, something like /usr/bin/grep.
  • type cd says cd 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 ./:

Bash
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:

Bash
export PATH="$HOME/terminal-demo:$PATH"
args.sh hello

PATH 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:

Bash
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:

Bash
cd ~/terminal-demo
grep " 500 " logs/app.log > errors.txt
cat errors.txt

>> adds to the end of a file instead of replacing it:

Bash
echo "checked the log" > notes.txt
echo "found 5 server errors" >> notes.txt
cat notes.txt

2> redirects standard error. Ask grep to read a file that does not exist and send its complaint to a file:

Bash
grep "500" logs/missing.log 2> grep-errors.txt || echo "exit code: $?"
cat grep-errors.txt

Check 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:

Bash
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 -c
  • grep " 500 " logs/app.log keeps only the lines with status 500. The spaces around 500 stop it matching a time such as 09:05:00.
  • wc -l counts lines, so the first command answers "how many server errors?"
  • cut -d " " -f 4 splits each line at spaces (-d sets the delimiter) and keeps the fourth field, the page.
  • sort puts identical pages next to each other.
  • uniq -c collapses each run of identical lines into one and puts the count in front. It only merges neighbouring lines, which is why sort comes 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?":

Bash
grep " 500 " logs/app.log | cut -d " " -f 4 | sort | uniq -c | sort -rn | head -n 1

sort -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:

Bash
sleep 60

Ctrl+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:

Bash
cat

cat 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:

Bash
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.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.