Shell Introduction
What is Shell?
Shell is the interface between the user and the Linux kernel. It receives commands entered by the user, interprets them, and passes them to the kernel for execution, then returns the results to the user.
┌──────────────────────────────────────────────────────────┐
│ User │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Shell │ │
│ │ • Command interpreter │ │
│ │ • Script programming environment │ │
│ │ • User interaction interface │ │
│ └─────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Linux Kernel │ │
│ │ • Process management │ │
│ │ • Memory management │ │
│ │ • File system │ │
│ │ • Device drivers │ │
│ └─────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ Hardware │
└──────────────────────────────────────────────────────────┘Shell Functions
1. Command Interpretation
Shell reads and executes user-entered commands:
$ ls -la /home
# Shell parses this command, calls the ls program, and passes the arguments -la and /home2. Script Programming
Shell is a script programming language that can write automation scripts:
#!/bin/bash
for file in *.txt; do
echo "Processing file: $file"
done3. Environment Management
Shell manages environment variables and working environment:
$ export PATH=$PATH:/new/path
$ echo $HOME4. Input/Output Redirection
Shell can redirect command input and output:
$ command > output.txt # Output redirection
$ command < input.txt # Input redirection
$ command1 | command2 # Pipe5. Job Control
Shell can manage background processes:
$ command & # Run in background
$ jobs # View jobs
$ fg # Bring to foregroundCommon Shells
Bash (Bourne Again Shell)
The most popular Shell, the default Shell for most Linux distributions.
Features
- Part of the GNU project
- Compatible with Bourne Shell (sh)
- Rich features: command completion, history, aliases
- Powerful scripting capabilities
# Check if using Bash
$ echo $BASH_VERSION
5.1.16(1)-releaseZsh (Z Shell)
A more feature-rich Shell, the default Shell for macOS.
Features
- Powerful auto-completion
- Theme and plugin support (Oh My Zsh)
- Spell correction
- Shared history
# Install Zsh
$ sudo apt install zsh
# Set as default Shell
$ chsh -s /bin/zshFish (Friendly Interactive Shell)
A user-friendly modern Shell.
Features
- Out-of-the-box syntax highlighting
- Intelligent auto-completion
- Web-based configuration
- Not POSIX compatible
# Install Fish
$ sudo apt install fish
# Run Fish
$ fishOther Shells
| Shell | Description |
|---|---|
| sh (Bourne Shell) | The earliest Unix Shell |
| csh (C Shell) | C-like syntax |
| tcsh | Enhanced version of csh |
| ksh (Korn Shell) | Commonly used in commercial Unix |
| dash | Default Shell for Debian system scripts |
Viewing and Switching Shells
View Current Shell
# Method 1: View $SHELL variable
$ echo $SHELL
/bin/bash
# Method 2: View current process
$ ps -p $$
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
# Method 3: View $0
$ echo $0
-bashView Available Shells
$ cat /etc/shells
/bin/sh
/bin/bash
/bin/zsh
/usr/bin/zsh
/bin/fishTemporary Shell Switch
# Start Zsh
$ zsh
# Start Fish
$ fish
# Exit back to original Shell
$ exitChange Default Shell
# Use chsh command
$ chsh -s /bin/zsh
# Or edit /etc/passwd (requires root)
$ sudo usermod -s /bin/zsh usernameBash Configuration Files
Bash uses multiple configuration files that are loaded in different scenarios.
Configuration File Overview
┌─────────────────────────────────────────────────────────────┐
│ Shell Startup Type │
├─────────────────────────────────────────────────────────────┤
│ │
│ Login Shell Non-Login Shell │
│ (Login Shell) (Non-login Shell) │
│ │ │ │
│ ▼ ▼ │
│ /etc/profile /etc/bash.bashrc │
│ │ │ │
│ ▼ ▼ │
│ ~/.bash_profile ~/.bashrc │
│ or ~/.bash_login │
│ or ~/.profile │
│ │
└─────────────────────────────────────────────────────────────┘Login Shell vs Non-Login Shell
Login Shell
- Starts when user logs into the system (TTY login, SSH login)
- Executes
/etc/profileand~/.bash_profile
Non-Login Shell
- Starts when opening a terminal emulator
- Executes
/etc/bash.bashrcand~/.bashrc
# Check if it's a login Shell
$ shopt -q login_shell && echo "Login Shell" || echo "Non-login Shell"
# Force login Shell
$ bash --login
$ bash -lMain Configuration Files
/etc/profile (System-level, at login)
# System-wide environment variable settings
# Executed when all users log in~/.bash_profile (User-level, at login)
# User's login configuration
# Usually contains:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi~/.bashrc (User-level, each startup)
# User's Shell configuration
# Aliases, functions, prompts, etc.
# Aliases
alias ll='ls -la'
alias grep='grep --color=auto'
# Environment variables
export EDITOR=vim
# Custom functions
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Prompt
PS1='\u@\h:\w\$ '~/.bash_logout (at exit)
# Commands to execute on logout
# For example, cleaning temporary filesLoading Configuration Files
# Reload .bashrc
$ source ~/.bashrc
$ . ~/.bashrc
# Reload .profile
$ source ~/.profileShell Prompt
PS1 - Main Prompt
# View current PS1
$ echo $PS1
\u@\h:\w\$
# Customize prompt
$ PS1="[\t] \u@\h:\w\$ "
[10:30:00] maxwell@ubuntu:~$Special Characters
| Character | Meaning |
|---|---|
\u | Username |
\h | Hostname |
\H | Full hostname |
\w | Current directory |
\W | Current directory name (without path) |
\d | Date |
\t | Time (24-hour) |
\T | Time (12-hour) |
\n | Newline |
\$ | Prompt (regular user $, root #) |
\[...\] | Non-printing character wrapper (for colors) |
Colored Prompt
# Color codes
# \e[color code]m start, \e[0m end
# Example: Green username, blue path
PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m\]\$ 'Common Color Codes
| Code | Color |
|---|---|
| 30 | Black |
| 31 | Red |
| 32 | Green |
| 33 | Yellow |
| 34 | Blue |
| 35 | Magenta |
| 36 | Cyan |
| 37 | White |
PS2 - Continuation Prompt
# Displayed when command is incomplete
$ echo "hello
> world"PS3 and PS4
# PS3 - Prompt for select statement
# PS4 - Prompt for debug modeBash Features
Command History
# View history
$ history
# History configuration
HISTSIZE=1000 # Number of commands saved in memory
HISTFILESIZE=2000 # Number of commands saved in history file
HISTCONTROL=ignoreboth # Ignore duplicate and space-prefixed commands
HISTIGNORE="ls:cd:pwd" # Ignore specific commandsCommand Completion
# Tab completion for files and commands
$ cd Doc<Tab>
$ cd Documents/
# Install enhanced completion
$ sudo apt install bash-completionAliases
# Define alias
$ alias ll='ls -la'
# View aliases
$ alias
# Delete alias
$ unalias ll
# Temporarily skip alias
$ \ls
$ command lsCommand Types
# Built-in commands: Commands built into Shell
$ type cd
cd is a shell builtin
# External commands: Independent executable files
$ type ls
ls is /usr/bin/ls
# Aliases
$ type ll
ll is aliased to 'ls -la'
# Functions
$ type mkcd
mkcd is a functionOh My Bash
Oh My Bash is a Bash configuration framework that provides themes and plugins.
Installation
$ bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"Configuration
Edit ~/.bashrc:
# Theme
OSH_THEME="powerline"
# Plugins
plugins=(git bashmarks)Summary
This chapter introduced the basic concepts of Shell:
- Shell's role: Command interpretation, script programming, environment management
- Common Shells: Bash, Zsh, Fish
- Configuration files:
.bashrc,.bash_profile, etc. - Prompt customization: PS1 and color settings
- Bash features: History, completion, aliases
Shell is a core tool in Linux; understanding it deeply will greatly improve your work efficiency. Next, we'll learn about input/output redirection.
Previous chapter: File Search
Next chapter: Input/Output Redirection