03. Terminal & Shell
While graphical interfaces (GUIs) exist in Linux, the true power, speed, and automation capabilities lie in the command line interface (CLI).
03.1 Terminal Concepts
Section titled “03.1 Terminal Concepts”Often used interchangeably, there is a technical difference between a terminal, a console, and a TTY.
Terminal & Console
Section titled “Terminal & Console”- Terminal: Originally a physical hardware device (a screen and keyboard) used to interact with a mainframe. Today, it refers to software (a “Terminal Emulator” like GNOME Terminal, iTerm2, or Windows Terminal) that provides a text-based window to interact with the shell.
- Console: The physical terminal directly connected to the host machine (e.g., the monitor and keyboard plugged into the server rack).
TTY & Pseudo-terminal (PTY)
Section titled “TTY & Pseudo-terminal (PTY)”- TTY (Teletypewriter): The subsystem in Linux that handles text input and output. When you log in locally, you get a TTY (usually TTY1 through TTY6).
- Pseudo-terminal (PTY): When you SSH into a server or open a terminal emulator window, you aren’t using physical hardware. The OS creates a virtual TTY, known as a
ptyorpts(Pseudo-Terminal Slave), to handle the connection.
03.2 The Shell
Section titled “03.2 The Shell”If the terminal is the window you type into, the Shell is the program running inside that window that interprets your commands and executes them.
Shell vs Terminal
Section titled “Shell vs Terminal”- Terminal: The graphical window you open.
- Shell: The engine processing your commands (Bash, Zsh).
Common Shells
Section titled “Common Shells”- Bash (Bourne Again Shell): The default shell on almost all Linux distributions. Highly compatible and ubiquitous.
- Zsh (Z Shell): Highly customizable and interactive. The default shell on macOS. Supports advanced auto-completion and themes (e.g., Oh My Zsh).
- Fish (Friendly Interactive Shell): Known for syntax highlighting and intelligent auto-suggestion out of the box, but not strictly POSIX-compliant (some bash scripts won’t run natively in fish).
03.3 Shell Basics
Section titled “03.3 Shell Basics”Commands, Arguments, and Options
Section titled “Commands, Arguments, and Options”A typical shell command follows this structure:
command -options argument- Command: The program to run (e.g.,
ls). - Options (Flags): Modifies the behavior of the command, usually starting with a
-(e.g.,-lfor long format,-afor all files). - Arguments: The target of the command (e.g., a file or directory name like
/var/log).
Example: ls -la /var/log
Absolute vs Relative Paths
Section titled “Absolute vs Relative Paths”- Absolute Paths: The full path starting from the root directory (
/).- Example:
cd /etc/ssh
- Example:
- Relative Paths: The path starting from your current working directory.
- Example: If you are in
/etc, typingcd sshtakes you to/etc/ssh. .means current directory...means one directory up (parent directory).
- Example: If you are in
Productivity Features
Section titled “Productivity Features”- Tab Completion: The most important shortcut in Linux. Start typing a file, directory, or command name, and press
Tab. The shell will automatically complete it. - History: The shell remembers your past commands.
- Type
historyto view them. - Use the
UpandDownarrow keys to cycle through previous commands. - Press
Ctrl + Rto reverse search your history.
- Type
- Aliases: Custom shortcuts for longer commands.
Terminal window # Create a temporary aliasalias ll='ls -la'# To make it permanent, add it to your ~/.bashrc or ~/.zshrc
03.4 Command Help
Section titled “03.4 Command Help”You do not need to memorize every command and flag. Linux has built-in manuals.
-
man(Manual Pages): The most comprehensive help system.Terminal window man ls(Press
qto quit the manual). -
help: Used for shell built-in commands (likecdoralias) that don’t have separatemanpages.Terminal window help cd -
--helpFlag: Most commands support this flag for a quick summary of options without opening the full manual pager.Terminal window mkdir --help -
info: A different format of documentation, often containing hyperlinked nodes. Sometimes more detailed thanmanfor GNU utilities.Terminal window info coreutils -
whatis: Displays a brief, one-line description of a command.Terminal window whatis grep -
apropos: Searches the manual page names and descriptions for a specific keyword. Perfect when you know what you want to do, but forgot the command name.Terminal window apropos "list directory"