Skip to content
Pwnversity
FUND-CLIbasic · 10 min · upd. 2026-11

The Terminal and the Shell

When you exploit a vulnerability you don’t get a desktop: you get a command line, sometimes with no history, no tab completion and no Ctrl+C. And on your own machine, the terminal is where you chain tools together to turn thousands of lines of output into the one piece of data you need. This page covers the basics: what each piece is, how to read a command and how commands are combined.

Piece What it is Examples
Terminal emulator The window: it takes keystrokes and draws text. It doesn’t understand commands. GNOME Terminal, iTerm2, Ghostty, Windows Terminal
Shell The interpreter: it reads the line, expands it, finds the program and launches it. bash, zsh, sh/dash, fish; on Windows, PowerShell and cmd
Program The executable that does the work. /usr/bin/ls, /usr/bin/nmap
  • bash is what almost every tutorial assumes; zsh is the default shell on Kali and macOS; on Debian and Kali, /bin/sh is dash, which is simpler and lacks many bash features.
  • Some commands aren’t programs but shell builtins (cd, export, umask, alias): that’s why which cd returns nothing.
ls -la /var/log
│ │ └ argument: what it acts on
│ └ options: short ones can be grouped (-l -a = -la); long ones use two dashes (--all)
└ command
To find out… Use
What a command does and all its options man ls (/text searches, n next, q quits)
The quick summary ls --help
Which command does something apropos "copy files"
Where the executable is which nmap
What it really is (program, builtin, alias, function) type ls
  • Absolute: starts with / and always leads to the same place (/etc/passwd).
  • Relative: depends on where you are (pwd tells you).
  • Shortcuts: . the current directory (that’s why you run ./script.sh), .. the one above, ~ your home folder, cd - goes back to the previous one.
  • Wildcards are expanded by the shell before the program runs: * anything, ? one character, [abc] one of those.

Every process has 0 input (stdin), 1 output (stdout) and 2 errors (stderr). By default, output and errors both go to the screen, mixed together.

Syntax What it does
> f · >> f Output to a file, overwriting · appending
2> f Only errors to the file
> f 2>&1 · &> f Output and errors to the same file
2>/dev/null Throw errors away (/dev/null discards everything)
< f Use a file as input
a | b Pipe: a’s output is b’s input. Errors don’t go through
a ; b b always
a && b · a || b b only if a succeeds · only if it fails

Every command ends with an exit code: 0 is success, any other number is failure. It’s in $? right afterwards.

flowchart LR
    A["find / -perm -4000"] -->|"stdout"| B["grep -v snap"]
    A -->|"stderr"| N["2>/dev/null"]
    B -->|"stdout"| C["sort"]
    C -->|">"| D["suid.txt"]
  • VAR=value (no spaces around =) creates a variable in the current shell; export VAR makes programs you launch inherit it; env lists them.
  • Double quotes "$VAR" expand the variable; single quotes '$VAR' keep it literal.
  • The PATH is the ordered list of directories where the shell looks for programs. The current directory is not in it (that’s why ./ is needed). Extend it with export PATH="$PATH:$HOME/bin" and, to make it stick, in your shell’s startup file.
Tool What for Example
grep Filter lines: -i case-insensitive, -v invert, -r recursive, -l names only, -E regex grep -ri password /etc 2>/dev/null
cut Cut columns by delimiter cut -d: -f1 /etc/passwd
sort · uniq Sort · remove consecutive duplicates (-c counts) sort | uniq -c | sort -rn
awk Columns and simple logic awk '{print $1}' access.log
sed Replace text on the fly sed 's/http:/https:/g'
wc Count lines, words, bytes wc -l hosts.txt
find Search files by name, type, size, owner, permissions find / -perm -4000 -type f 2>/dev/null
head · tail · less Start · end (-f follows the file) · page through tail -f /var/log/auth.log

For finer regular expressions, see regex.

Victim shells: from “dumb” to interactive

Section titled “Victim shells: from “dumb” to interactive”

A freshly caught reverse shell usually has no TTY: no history or tab completion, Ctrl+C kills it, and sudo or su don’t work properly. The classic upgrade:

Ventana de terminal
# 1. In the victim's shell: create a pseudo-terminal
python3 -c 'import pty; pty.spawn("/bin/bash")' # or: script -qc /bin/bash /dev/null
# 2. Background it with Ctrl+Z and, in YOUR terminal:
stty raw -echo; fg
# 3. Back in the victim:
export TERM=xterm
stty rows 40 columns 160 # match your window (in your terminal: stty size)

More variants and how to catch them in reverse shells.

Ventana de terminal
# Get your bearings on a new machine
whoami; id; hostname
uname -a; cat /etc/os-release
ip -br a; df -h; free -h
# A typical chain: users with a valid shell, sorted
grep -vE 'nologin|false' /etc/passwd | cut -d: -f1,7 | sort
# How many accounts use each shell
cut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn
# Save output and see it at the same time
nmap -sV 10.10.10.5 | tee nmap.txt
Concept What to remember
Terminal · shell · program Window · interpreter · executable.
Builtin Part of the shell (cd, export): which can’t find it; type can.
.zshrc · .bashrc Your shell’s one. Kali uses zsh.
> · >> Overwrites · appends.
2>/dev/null Gets rid of error noise.
| Only passes output, not errors.
&& · ; Only on success · always.
$? Exit code: 0 = success.
sudo and > Your shell does the redirection. Use sudo tee.
uniq Only looks at the previous line: always after sort.
PATH No .. “Command not found” is usually the PATH.
Dumb shell pty.spawn → Ctrl+Z → stty raw -echo; fg → TERM.
  1. Why does which cd return nothing?

    Answer

    Because cd is a shell builtin, not a program on disk. type cd identifies it. It has to be a builtin: an external program couldn’t change the directory of the shell that launched it.

  2. With ls /etc /nonexistent > f.txt, what stays on screen and what goes to the file? And with ls /etc /nonexistent > f.txt 2>&1?

    Answer

    In the first, the /etc listing goes to the file and the /nonexistent error shows on screen. In the second, both go to the file and nothing shows on screen.

  3. Why does sudo echo hello > /etc/test give “permission denied”, and how do you fix it?

    Answer

    Because your unprivileged shell performs the redirection before running sudo. Fix it with echo hello | sudo tee /etc/test.

  4. sort -u and sort | uniq give the same result, but uniq alone, without sort, doesn’t remove all duplicates. Why?

    Answer

    Because uniq only compares each line with the previous one: it removes consecutive duplicates. If the input isn’t sorted, separated repeats remain.

  5. You installed a tool with go install and get “command not found”. What do you check?

    Answer

    That the binary is in ~/go/bin and that the directory is in the PATH. If it isn’t, add it with export PATH="$PATH:$HOME/go/bin" in your shell’s startup file.

  6. You catch a reverse shell and pressing Ctrl+C closes it. What’s missing, and how do you get it?

    Answer

    A TTY. Create a pseudo-terminal on the victim (python3 -c 'import pty; pty.spawn("/bin/bash")'), background it with Ctrl+Z, run stty raw -echo; fg in your terminal, and set TERM and the size.