Last modified: March 14, 2023
This article is written in: 🇺🇸
A Unix shell is a command-line interpreter that provides a user interface for accessing an operating system's services. It allows users to execute commands, run programs, and manage system resources. The shell acts as an intermediary between the user and the operating system kernel, translating user commands into actions performed by the system.
The interaction between the user, shell, and operating system can be visualized as follows:
+-------------------+ +----------------+ +--------------------+
| | | | | |
| User Input |<----->| Shell |<----->| Operating System |
| (Keyboard/Screen) | | (e.g., Bash) | | (Kernel/HW) |
| | | | | |
+-------------------+ +----------------+ +--------------------+
There are several types of shells available, each with unique features:
Shell | Description | Benefits | Considerations/Drawbacks |
bash (Bourne-Again SHell) |
The default shell on most Linux distributions; backward-compatible with the original Bourne shell. | Widely used, with extensive scripting support and community resources. | Lacks some advanced features present in newer shells like zsh . |
zsh (Z Shell) |
Known for its rich feature set, including improved auto-completion, spell correction, and theming capabilities. | Highly customizable, with better autocompletion and plugins. | Slight learning curve for users unfamiliar with its configuration. |
ksh (Korn SHell) |
Combines features of the Bourne shell and the C shell (csh ). |
Useful for scripting, combining the best of both worlds (Bourne and C shell). | Not as widely adopted as bash or zsh . |
tcsh (TENEX C Shell) |
An enhanced version of the C shell, featuring command-line editing and programmable word completion. | Better user experience with command-line editing features. | Less common compared to bash or zsh . |
sh (Bourne SHell) |
The original Unix shell, simple and portable. | Lightweight and portable for basic scripting tasks. | Lacks many modern features available in newer shells. |
To see which shells are installed on your system, inspect the /etc/shells
file. This file lists all the valid login shells available.
cat /etc/shells
Example Output:
/bin/sh
/bin/bash
/bin/dash
/bin/zsh
/usr/bin/zsh
To determine your current active shell, you can use several methods:
$SHELL
Variable
echo "$SHELL"
Note: The $SHELL
variable shows your default login shell, not necessarily the shell you're currently using.
ps -p "$$" -o comm=
$$
represents the current shell's process ID.ps -p
selects the process with that ID.-o comm=
outputs the command name (the shell).echo "$0"
echo "$0"
$0
contains the name of the shell or script being executed.You can start a different shell session by typing its name:
zsh
To return to your previous shell, type exit
or press Ctrl+D
.
To change your default login shell, use the chsh
(change shell) command:
chsh -s /bin/zsh
Important: The shell must be listed in /etc/shells
; otherwise, chsh
will not accept it.
When Bash starts, it reads and executes commands from various startup files. These files allow you to customize your shell environment.
Understanding which configuration files are read depends on how the shell is invoked:
I. Global Configuration Files (affect all users):
/etc/profile
: Executed for login shells./etc/bash.bashrc
or /etc/bashrc
: Executed for interactive non-login shells.II. User-Specific Configuration Files (affect only the current user):
~/.bash_profile
or ~/.bash_login
or ~/.profile
: Read by login shells. Bash reads the first one it finds.~/.bashrc
: Read by interactive non-login shells.~/.bash_logout
: Executed when a login shell exits./etc/profile
.~/.bash_profile
, ~/.bash_login
, and ~/.profile
(in that order) and reads the first one it finds./etc/bash.bashrc
or /etc/bashrc
(system-wide configuration).~/.bashrc
(user-specific configuration).~/.bashrc
from ~/.bash_profile
To ensure that your settings are consistent across all shell types, it's common to source ~/.bashrc
from ~/.bash_profile
.
Example ~/.bash_profile
:
# ~/.bash_profile
# Source the user's bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
~/.bashrc
File
# ~/.bashrc
# Source global definitions if any
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Alias definitions
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Environment variables
export EDITOR='nano'
export HISTSIZE=1000
export HISTFILESIZE=2000
# Prompt customization
PS1='\u@\h:\w\$ '
# Functions
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1";;
*.7z) 7z x "$1" ;;
*) echo "Don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
alias ll='ls -alF'
is an example that lists all files in long format, including indicators for file types.export EDITOR='nano'
ensures that nano becomes the default text editor when editing files through the terminal.PS1='\u@\h:\w\$ '
modifies the prompt to show the username, hostname, and the current working directory.extract()
is useful for extracting different archive types such as .zip
, .tar.gz
, and .rar
files, making file management more efficient.A terminal emulator is a program that emulates a physical terminal within a graphical interface, allowing users to interact with the shell.
Terminal Emulator | Description | Benefits | Considerations/Drawbacks |
GNOME Terminal | Default terminal emulator on GNOME desktop environments. | Integrated with GNOME, easy to use. | Lacks some advanced customization features. |
Konsole | Default terminal emulator on KDE Plasma desktop environments. | Highly customizable and integrates well with KDE. | Primarily designed for KDE, may not be ideal for other environments. |
xterm | Basic terminal emulator for the X Window System. | Lightweight and highly portable. | Lacks modern features like tabs or split views. |
Terminator | Allows arranging multiple terminals in grids. | Ideal for multitasking with a grid layout. | May be overkill for basic terminal usage. |
iTerm2 | Popular terminal emulator for macOS with advanced features. | Offers split panes, hotkeys, and extensive customization. | Only available on macOS. |
Ctrl + Alt + T
(commonly opens the default terminal).cat
. Use alias cat
to see the aliases for cat
.alias
without any arguments.~/.bashrc
in a text editor, add a new alias like alias ll='ls -la'
. Save the file, reopen your terminal, and verify the new alias. To remove it, delete or comment out the line in ~/.bashrc
, then save and restart your terminal.find
command to search your system for files containing 'profile' in their name. Try find / -name '*profile*'
.useradd -s /bin/tar username
creates a user with /bin/tar
as their shell. Be aware of the implications this may have on user interaction with the system.chsh -s /path/to/shell
, then open a new terminal session and explore the new environment. Experiment with commands like alias
, set
, and declare -f
to inspect custom variables, aliases, and functions.