← Back to Linux

The shell and command basics

The shell is a program that reads commands and runs them. Learning its syntax unlocks Linux's power, because almost everything can be done, and automated, from the command line.

What a shell is

A shell is a command interpreter: it displays a prompt, reads a line, expands it, runs programs, and shows their output. Bash, the GNU Bourne-Again Shell first released in 1989, is the default on many distributions; Zsh is the default on macOS and popular with power users; Dash is a fast minimal shell used for system scripts on Debian and Ubuntu. A terminal emulator is the window in which a shell runs.

Command structure

A command line has a command name, options, and arguments: ls -l /etc. Short options use one dash and can be combined (ls -la); long options use two dashes (ls --all). Arguments are often file names. Spaces separate words, so names containing spaces must be quoted: cat 'my notes.txt'.

Getting help

man ls opens the manual page for ls, with sections for user commands (1), system calls (2), library functions (3), file formats (5), and administration commands (8), as in man 5 passwd. Many commands accept --help for a short summary, and info and online documentation give more depth. apropos keyword searches manual page descriptions.

Variables and the environment

Shell variables are set with name=value (no spaces around =) and read with $name: greeting=hello; echo $greeting. export makes a variable part of the environment passed to child processes. Important environment variables include PATH (directories searched for commands), HOME, USER, SHELL, and LANG. env or printenv lists the environment.

PATH and finding commands

When you type a command without a slash, the shell searches the directories in PATH, in order, for an executable with that name. Builtins such as cd and echo are part of the shell itself. type command shows whether a name is a builtin, alias, function, or file; to run a program in the current directory, use ./program, since . is usually not in PATH for safety.

Expansion and quoting

Before running a command, the shell expands it: ~ becomes the home directory; $VAR is replaced by its value; $(command) is replaced by a command's output; {a,b} produces multiple words; and globs such as *.txt match file names. Single quotes prevent all expansion; double quotes allow variable and command substitution but prevent word splitting and globbing. Quoting variables, as in "$file", avoids many bugs.

History and editing

The shell records commands in a history: history lists them, the up arrow recalls them, Ctrl+R searches backwards, and !! repeats the last command. Tab completes commands and file names. Ctrl+C interrupts the running command, Ctrl+D signals end of input or exits the shell, Ctrl+L clears the screen, and aliases such as alias ll='ls -l' create shortcuts.

Worked example

Suppose a directory contains report 2025.txt and notes.txt. Running for f in *.txt; do wc -l $f; done fails on the first file: the unquoted $f splits into two words, so wc looks for files named report and 2025.txt, which do not exist. Writing wc -l "$f" passes the name as one word and works. Similarly, rm $f would try to delete files named report and 2025.txt, while rm "$f" deletes the intended file.

Key takeaways

A shell reads, expands, and runs commands in a terminal; Bash (1989) is common, with Zsh and Dash alternatives. Commands take short options (-l), long options (--all), and arguments; quote names with spaces. man, --help, and apropos provide help, with manual sections such as 1, 5, and 8. Variables are set with name=value, exported to the environment, and read with $name; PATH lists where commands are searched. type identifies builtins, aliases, and files; run local programs with ./. The shell expands ~, variables, $(...), braces, and globs; quote variables. History, Tab completion, Ctrl+R, Ctrl+C, and aliases speed work.

Flashcard

Shell

Command interpreter

Bash, Zsh.

Check your understanding

Why does echo '$HOME' print $HOME literally, while echo "$HOME" prints /home/ana?

← Previous lesson Next lesson →