Comprehensive Shell Guide
Comprehensive Overview of the Shell
A shell serves as the intermediate interface between a computer's operating system (OS) and the user. It is physically named a "shell" because it represents the "outer layer" that surrounds the core of the operating system.
There are two primary styles or "flavors" of shell interfaces:
Graphical User Interface (GUI): Often referred to by the phonetic acronym "gooey." This interface is characterized by visual elements and a "clicky clicky" interaction style where the user employs a mouse to navigate.
Command-Line Interface (CLI): Often referred to by the acronym "see-ell-eye." This interface is characterized by a "typey typey" interaction style where the user enters text-based commands.
The functions facilitated by a shell include:
File Management: Organizing, moving, and editing files.
Program Control: Starting and stopping various software applications.
OS Configuration: Adjusting the settings and parameters of the operating system.
Automation: Dr. Jacob Hochstetler notes that a primary goal is to "automate all the things."
Technical Terminology and Distinctions
While the terms "Terminal," "Shell," and "CLI" are frequently used interchangeably in common parlance, they have specific technical definitions:
Terminal: This refers specifically to the text-only window that is running a shell program.
Shell: This is the underlying program that exposes the operating system's functions to the user. In Linux, the shell is specifically defined as a command-line interpreter. In Windows,
cmdandPowerShellserve as the primary command-line interpreters.CLI (Command-Line Interface): This is the interface that specifically processes user-inputted commands and outputs the resulting data.
Command Execution and the Engineer’s Philosophy
Command execution in the shell can be approached in two ways:
GUI Method: Clicking through windows and dialog boxes using a mouse.
CLI Method: Typing program names and arguments directly into a console.
Dr. Hochstetler emphasizes a focus on the CLI because it is extremely difficult to automate the process of clicking through windows. He states that as an engineer, he is "inherently lazy," which serves as a motivation to automate every possible task. The shell also allows for the use of "fancy CLI environment variable substitution," which simplifies complex tasks in a way that has no clear equivalent in a GUI.
Builtin versus External Commands
The shell categorizes commands into two distinct types:
Builtin Commands: These are commands that are built directly into the shell program itself. They are typically essential operations that the shell requires to function.
External Commands: These are programs that exist outside of the shell. They are not part of the internal shell code.
Some commands may exist as both a builtin and an external command to ensure compatibility across different environments. Examples include:
Linux/UNIX (macOS):
cd(Bash)echo(Bash)alias(Zsh)setopt(Zsh)
Windows:
dir(cmd.exe)copy(cmd.exe)Get-Help(PowerShell)Set-Location(PowerShell)
Accessing the Shell Programmatically
Accessing the shell varies depending on the specific operating system and environment:
Windows:
, then
, then type
wt(Windows Terminal)
macOS:
, then type "term"
Note: "iTerm" is considered a superior terminal replacement for macOS, supporting a
^~hotkey.
Linux:
through (supported by some distributions)
Directory Navigation and File Management
The shell provides core commands to navigate the file system:
cd: Change Directory. This command moves the user to a different folder..(single dot): Represents the current directory...(two dots): Represents the parent directory (one level up).
pwd: Print Working Directory. This displays the full path of the current location.ls: List Files. This displays the contents of the current directory.
Note: In shell scripts or command displays, the pound sign (#) is used to denote comments that explain the output.
Standard Streams and Redirection Mechanics
The shell interacts with the operating system through three standard streams:
stdin (Standard Input): The input provided to the program, typically originating from the keyboard.
stdout (Standard Output): The output generated by the program, typically displayed on the screen or console.
stderr (Standard Error): Error messages generated by the program, which are kept separate from the standard output.
These streams enable the manipulation of data through redirection and piping:
>: Redirects program output to a file, overwriting any existing content in that file.>>: Redirects program output to a file, appending the new data to the end of the existing content.|: Known as a "pipe," this sends the output of one program directly into the input of another program.
Example Scenario: An operator can pipe the output of the ls command into the sort command, and then redirect that sorted output into a file named out.txt.
Environment Variables (ENVs)
Environment variables are dynamic values that store session-specific or system-wide information utilized by the shell and other processes.
Common ENVs in Linux and macOS:
: Contains a list of directories that the shell searches through whenever it needs to find executable programs.
: Stores the name of the user currently logged into the session.
: Stores the path to the current user's home directory.
Displaying Environment Variables:
Linux/macOS:
echo $VAR_NAMEWindows:
echo %VAR_NAME%
Setting Environment Variables:
Linux/macOS:
export VAR_NAME=newValueWindows:
set VAR_NAME=newValue
Essential Shell Shortcuts
Name | Shell | Function |
|---|---|---|
| Bash/Zsh | Initiates a reverse history search to find previous commands. |
| Bash/Zsh | Moves the cursor immediately to the beginning of the line. |
| Bash/Zsh | Moves the cursor immediately to the end of the line. |
| Bash/Zsh | Cuts all text from the current cursor position to the end of the line. |
| Bash/Zsh | Pastes (yanks) the most recently cut text. |
| Bash/Zsh | Executes the last command again. |
| Bash/Zsh/Windows | Scrolls through the history of previous commands. |
| Windows | Repeats the last command entered. |
| Bash/Zsh/Windows | Autocompletes commands, file names, or directory paths. |
| Bash/Zsh/Windows | Cancels or terminates the command currently running. |
Essential Commands and Programs
Name | Operating System | Function |
|---|---|---|
| Linux/macOS/Windows | Displays text or the specific value of a variable in the terminal. |
| Linux/macOS | Opens the manual page (documentation) for a specific command. |
| Linux/macOS | Concatenates files and displays their contents. |
| Linux/macOS | Downloads web pages or data from the internet. |
| Linux/macOS | Searches for a specific string of text within a file. |
| Linux/macOS | Locates files that match a specific pattern. |
| Linux/macOS | Terminates a specific running process. |
| Linux/macOS | Provides a real-time display of system resource usage. |
| Windows | Searches for text strings within files. |
| Windows | Outputs the contents of a file to stdout. |
Software Installation and Package Management
Package Management is defined as the formal process of installing, configuring, upgrading, and removing software on a system.
APT (Advanced Package Tool): Used by Debian-based Linux distributions, such as Ubuntu.
sudo apt update: Updates the local list of available packages.sudo apt install [package_name]: Installs a specific software package.
YUM/DNF: Used by RHEL-based (Red Hat Enterprise Linux) distributions.
sudo yum update: Updates currently installed packages.sudo yum install [package_name]: Installs a specific software package.
Brew (Homebrew): Used on macOS; requires manual installation first.
brew install [program_name]: Installs a specific software program.
Chocolatey: Used on Windows; requires manual installation first.
choco install [program_name]: Installs a specific software program.