Linux shells serve as an interface to the OS, a command-line interpreter, and a scripting language.
There
are multiple shells available, and different ones offer different features.
Linux shells are computer programs, just like web browsers. You can choose which shell you
prefer based on features, familiarity, or specific needs—just like picking a browser (e.g., Firefox,
Chrome, Edge).
A Shell is Three Things:
An interface to the OS – It allows users to interact with the system, primarily through
the command line.
A command-line interpreter – It processes and executes user commands.
A scripting language – It allows users to write scripts to automate tasks.
There are multiple shells available, and you can check which ones are installed on your system
using:
cat /etc/shells
This command will list available shells, such as:
/bin/sh
/bin/bash
/bin/dash
/bin/zsh
/bin/fish
Each shell has different features. Bash, the most common default shell, has advanced features
like command history, tab completion, and scripting enhancements. Other shells, like sh, are
more basic but offer better portability.
Comparing Bash with sh (Bourne Shell)
Feature Bash (Bourne Again Shell) sh (Bourne Shell)
Supports tab completion for commands, file paths,
Tab Completion No tab completion support.
and variables.
Stores command history, allowing navigation with
Command History Limited or no history support.
Up and Down arrows or history command.
Aliases Supports alias for creating shortcuts for commands. No alias support.
Supports one-dimensional indexed and associative
Arrays No array support.
arrays.
Arithmetic Supports (( expression )) and $(( expression )) for Only supports expr or $[ ] for
Expansion arithmetic operations. basic arithmetic.
Feature Bash (Bourne Again Shell) sh (Bourne Shell)
String Provides powerful string manipulation with $ Very limited string
Manipulation {VAR##pattern} and ${VAR%%pattern}. manipulation features.
Scripting Offers functions, [[ ... ]] tests, =~ regex matching, Basic scripting capabilities with
Enhancements and improved loops. [ ... ] test conditions.
Process Allows >(command) and <(command) for advanced
Not supported.
Substitution piping.
Brace Expansion Supports {a,b,c} to generate sequences. Not supported.
Allows job suspension (Ctrl+Z), backgrounding (&),
Job Control Minimal job control support.
and fg/bg commands.
Key Differences in Use Cases
sh (Bourne Shell): Designed for portability and simplicity. It lacks advanced features but is
widely used for system scripts that need compatibility across different UNIX-like systems.
Bash (Bourne Again Shell): An improvement over sh with additional interactive and scripting
features, making it the default choice for most Linux distributions.
While sh scripts are more portable, bash is more user-friendly and powerful for both interactive use and
complex scripting.
Comparing Bash with sh (Bourne Shell)
You can switch between bash and sh easily and test their differences. Here’s how:
Switching Between Bash and sh
To switch from bash to sh:
sh
o This starts a new sh shell session.
To switch from sh back to bash:
exit # Or press Ctrl+D to exit sh
bash
o This starts a new bash shell session.
Testing Features in bash vs sh
1. Test Command History (history)
In bash, run:
history
o You’ll see a list of previous commands.
In sh, run:
history
o This might not work or may show very limited results, as sh lacks proper history
support.
2. Test Tab Completion
In bash, type:
ls /etc/sh<TAB>
o Bash will autocomplete to /etc/shells if available.
In sh, try the same, and tab completion won’t work.
3. Test Clearing the Screen
In both shells, the clear command works:
clear
However, in bash, you can also use:
clear -L
o This clears the screen but keeps the last command visible at the top (a Bash-
specific feature).
In sh, clear -L will return:
clear: illegal option -- L
Conclusion
Bash provides a better user experience with features like:
✅ Command history
✅ Tab completion
✅ Advanced clear options
Sh is more basic but lightweight and ideal for scripts needing broad compatibility.
When Linux starts and you log in, the bash shell is launched by default (unless another shell is
set as the default). Bash then listens for any commands you type and executes them. These
commands are referred to as shell commands.
Most of the essential shell commands you use daily—like ls, cp, mv, rm, cat, and echo—come
from the coreutils package.
The coreutils package is installed by default as part of your Linux software installation
because it contains essential shell commands.
Why Coreutils is Essential
The coreutils package includes fundamental commands such as:
File operations: cp, mv, rm, ls, touch, mkdir, rmdir
Text processing: cat, echo, cut, sort, uniq, wc
System utilities: whoami, uptime, df, du, pwd, chmod, chown
Without coreutils, many basic shell commands wouldn’t work, making the system almost
unusable. Since coreutils is a core system component, removing it can break your system, so it’s rarely
(if ever) removed intentionally.
Confirming Coreutils is Installed
Since coreutils is installed by default, you can verify its presence on your system:
Checking if Coreutils is Installed (RHEL-based Systems: CentOS, Rocky Linux,
Fedora, etc.)
You can use rpm and yum to query the system for the installed coreutils package:
1. Using rpm:
rpm -q coreutils
o If installed, it returns something like:
coreutils-8.32-31.el9.x86_64
o If not installed, it returns:
kotlin
package coreutils is not installed
2. Using yum (or dnf on newer systems):
yum list installed coreutils
or
dnf list installed coreutils
Checking if Coreutils is Installed (Debian-based Systems: Ubuntu, Debian)
For Debian-based systems, use dpkg and apt:
1. Using dpkg:
dpkg -l | grep coreutils
o If installed, it returns something like:
nginx
CopyEdit
ii coreutils 8.32-4ubuntu2 amd64 GNU core utilities
2. Using apt:
apt list --installed | grep coreutils
Conclusion
The coreutils package provides basic shell commands essential for Linux.
You can check if it's installed using rpm/yum (RHEL-based) or dpkg/apt (Debian-
based).
If coreutils is missing, many shell commands won’t work since they are part of this
package.
Shell Command Syntax Overview
In Linux, a shell command follows a particular structure. Understanding the syntax will help
you effectively use the terminal. Here’s a breakdown of common shell command syntax:
Basic Shell Command Structure
A typical shell command consists of the following components:
command [options] [arguments]
command: The program or utility you want to execute (e.g., ls, cp, mkdir).
options: Flags or switches that modify the behavior of the command (e.g., -l, -r, -v).
arguments: Files or resources on which the command operates (e.g., files or directories).
Example:
bash
CopyEdit
ls -l /home/user
ls: Command (list directory contents).
-l: Option (long listing format).
/home/user: Argument (directory to list).
Common Command Syntax Components
1. Commands
o A command is typically the program or utility name that you want to run.
o Examples: ls, mkdir, cp, rm, echo, etc.
2. Options (Flags or Switches)
o Options modify how the command behaves.
o Options usually begin with a single dash (-) for single-letter options (e.g., -l, -a) or
double dash (--) for longer, more descriptive options (e.g., --help, --version).
o Some commands support combined options (e.g., -al for -a and -l).
Example:
bash
CopyEdit
ls -l /home/user # `-l` is an option for long listing format
3. Arguments
o Arguments specify the files or directories to operate on.
o Arguments can be paths to files or directories (e.g., /home/user, file.txt).
Example:
bash
CopyEdit
cp file1.txt /home/user/ # `file1.txt` is the source file and
`/home/user/` is the destination directory
Extended Command Syntax Examples
1. Command with Multiple Options
You can combine options in a single command line.
ls -al /home/user
-a: Show hidden files.
-l: Use the long listing format.
2. Command with Arguments
Commands often require one or more arguments to specify what they should operate on.
cp file1.txt /home/user/
file1.txt: The source file.
/home/user/: The destination directory.
3. Command with Redirection
Redirection allows you to send the output of a command to a file or device instead of displaying
it on the terminal.
Output redirection (> or >>):
echo "Hello World" > hello.txt # Write to hello.txt, overwriting the
file
echo "New Line" >> hello.txt # Append to hello.txt
Input redirection (<):
sort < unsorted.txt # Read the input from unsorted.txt
4. Command with Pipe (|)
Pipes allow you to send the output of one command as the input to another command.
ps aux | grep "python"
ps aux: Shows the list of running processes.
|: Sends the output of ps aux to grep for filtering.
grep "python": Searches for the word "python" in the process list.
Special Characters in Shell Command Syntax
1. Wildcards:
o Wildcards are used to match multiple files or directories.
*: Matches any number of characters.
?: Matches a single character.
[]: Matches any character within the brackets.
Examples:
ls *.txt # List all `.txt` files in the current directory
ls file?.txt # List files like file1.txt, file2.txt, etc.
ls file[1-3].txt # List file1.txt, file2.txt, file3.txt
2. Semicolon (;):
o Allows multiple commands to be executed in sequence, one after the other.
cd /home/user; ls -l
3. Double Ampersand (&&):
o Executes the second command only if the first command succeeds (returns exit status
0).
mkdir new_dir && cd new_dir
4. Double Pipe (||):
o Executes the second command only if the first command fails (non-zero exit status).
mkdir new_dir || echo "Directory already exists"
5. Parentheses (()):
o Groups commands together, often used in subshells.
(cd /home/user && ls)
Exit Status of Commands
Every command in Linux returns an exit status, which indicates whether the command
succeeded or failed:
Exit status 0 means the command was successful.
Non-zero exit status indicates an error or failure.
You can check the exit status of the last command with:
echo $?
Examples of Command Syntax
mkdir mydir # Creates a directory called "mydir"
ls -l /home/user # Lists files in /home/user in long format
cat file.txt # Displays the contents of file.txt
rm file.txt # Removes file.txt
Summary
Commands are the basic unit for performing tasks.
Options modify the behavior of the command.
Arguments specify the targets for the command.
Special characters like *, &&, and || offer enhanced control over commands.
Redirection and pipes extend command functionality for more complex tasks.
Mastering shell command syntax is essential for efficient Linux administration and shell
scripting.
The man Page and How to Use It with Common Linux Commands
The man (manual) page is the built-in documentation system in Linux. It provides detailed
information about commands, utilities, system calls, and configuration files. You can access the
manual pages using the man command, followed by the command you want to learn about.
Basic Usage of man
The syntax to access the manual page for a command is:
man [command]
For example:
man ls
This will open the manual page for the ls command.
How to Navigate man Pages
Once you open a man page, you can use the following keys to navigate:
Arrow keys: Scroll line by line.
Page Up / Page Down: Scroll by page.
/searchterm: Search for a term within the man page (use n to jump to the next occurrence).
q: Quit the man page and return to the terminal.
Using man with Common Linux Commands
1. rpm (RPM Package Manager)
o rpm is used to manage packages on RPM-based distributions like CentOS, RHEL, and
Fedora.
o To view the man page for rpm, use:
bash
CopyEdit
man rpm
o Example usage of rpm:
Query installed packages:
bash
CopyEdit
rpm -qa
Install a package:
bash
CopyEdit
rpm -ivh package.rpm
Uninstall a package:
bash
CopyEdit
rpm -e package_name
2. yum (Yellowdog Updater, Modified)
o yum is the package manager for RHEL/CentOS systems (up to version 7) used for
installing, updating, and removing packages.
o To access the man page for yum, use:
bash
CopyEdit
man yum
o Example usage of yum:
Install a package:
yum install package_name
Update a package:
yum update package_name
Search for a package:
yum search package_name
3. dnf (Dandified YUM)
o dnf is the successor to yum in CentOS 8 and Fedora. It provides a more efficient way to
manage packages.
o To access the man page for dnf, use:
man dnf
o Example usage of dnf:
Install a package:
dnf install package_name
Update the system:
dnf update
Remove a package:
dnf remove package_name
4. ls (List Directory Contents)
o ls is used to list files and directories in the current directory.
o To access the man page for ls, use:
man ls
o Example usage of ls:
List files in long format:
ls -l
List all files including hidden:
ls -a
List files in human-readable sizes:
ls -lh
5. cd (Change Directory)
o cd is used to change the current working directory.
o To access the man page for cd, use:
man cd
Note: The cd command does not have a man page on some systems because it is a
shell built-in command, not a standalone program. Instead, you can use help cd
in a shell or refer to the shell’s documentation.
o Example usage of cd:
Change to a directory:
cd /path/to/directory
Move up one directory:
cd ..
6. pwd (Print Working Directory)
o pwd shows the full path of the current directory you are in.
o To access the man page for pwd, use:
man pwd
o Example usage of pwd:
Display the current directory:
pwd
Advanced Usage of man
You can use man with various options to customize the way it displays information:
Show a specific section: If there are multiple man pages for a command (e.g., a system
call, library function, etc.), you can specify the section.
man 2 open
This would show the open() system call, found in section 2 of the man pages.
Search within the man pages: Use the -k option to search for a keyword across all man
pages:
man -k keyword
For example, to search for commands related to "file":
man -k file
Summary
The man page is an essential tool for learning and exploring Linux commands. You can use it
with commands like rpm, yum, dnf, ls, cd, and pwd to get more information on their syntax,
options, and usage. Remember:
Use man [command] to open the manual.
Navigate with arrow keys, / for search, and q to quit.
Explore the options to refine your command usage and understand different behaviors and
functionality.
The man page is a valuable resource for Linux users of all levels!