What is a shell?
Computer software known as a shell makes an operating system’s services accessible to users or other programs.
A shell is a program that receives commands and sends them to the operating system for processing, to put it simply. In an interactive session, the user has the option of typing commands from the keyboard, or they can be written in a shell script that can be reused. On a Unix-type system such as Linux in the past, it was the sole user interface (UI) accessible. Today, in addition to command-line interfaces (CLIs) such as shells, we also have graphical UIs (GUIs).
The fundamental capability of shells is the ability to launch command-line programs that are already installed on the system. They also offer built-ins and scripting control structures such as conditionals and loops. Each shell has its own way of doing that. Some shells still support the Bourne shell, one of the original shells that was created for an early Unix system by a programmer named Steve Bourne and later standardized in the Portable Operating System Interface (POSIX) standard. Other projects, such as csh
/tcsh
, zsh
, and fish
, purposefully utilize a different syntax.
In order to use command-line shells, a user must be knowledgeable about commands, their calling syntax, and the fundamentals of the shell’s specific scripting language.
A Linux user can utilize a variety of shells, including the following:
sh
: A POSIX-compatible Bourne shell. In modern distros, it’s usually just Bourne again shell (Bash) running in compatibility mode.csh
/tcsh
: These come from the Berkeley Software Distribution (BSD) Unix system family but are also available on Linux; their scripting syntax is similar to that of C, and they are incompatible with the Bourne shell.ksh
: A Bourne shell derivative that was once very popular.bash
: Bash is the most common Linux shell and was created for the GNU project.zsh
andfish
: These are highly customizable and feature-rich shells that are intentionally different fromsh
derivatives and require learning, but have large communities of enthusiasts.
They all share similar properties, but each has its own unique attributes.
In this book, we will assume you are using Bash since it’s the default in most Linux distributions.
The Unix shell and Bash command language were both developed by Brian Fox for the GNU project. These were intended to serve as free software replacements for the Bourne shell. Since its introduction in 1989, it has remained the default login shell for the vast majority of Linux distributions. Linus Torvalds ported Bash and the GNU Compiler Collection (GCC) to Linux as one of the initial applications.
Bash has the following features:
- The shell will check to see whether a command is built in before searching through a list of directories to locate the program if not. This set is known as the search path. By running the
echo $PATH
command in Bash, you can view it. Thehome
directory and its subdirectory are included in the search path in addition to the current directory. You are able to create your own programs and call them up just by inputting their names. No matter which directory you are now in, a program such as this will be found and launched if it is stored in thebin
directory. We will find out more about the Linux directory structure in Chapter 3, The Linux Filesystem. - As with other Linux programs, the shell has a current directory linked to it. When looking for files, Linux-based programs begin in the current directory. To move the current directory to another location in the Linux filesystem, use the
cd
shell command. The current working directory is typically visible in the command prompt of modern shells. To check the version of your shell, run theecho $SHELL
command. You will get an output such as/bin/bash
. - A command is executed by designating it. The majority of Linux commands are just programs that the shell runs. For instance, the following
ls
command scans the current directory and lists the names of its files:ls -la
. - Commands frequently have argument strings that could, for example, be filenames. For instance, the following command switches to the
tmp
directory in yourhome
directory. The shell interprets the tilde character as yourhome
directory:cd ~/tmp
- Multiple arguments are required for some commands. The copy command, for instance, requires two arguments: the file to copy and its destination. This is demonstrated as follows by copying
file1
to a new file,file2
:cp file1 file2
- The flag or option argument strings for some commands typically start with
-
. The flags change how the invoked application behaves. When the following command is used,ls
outputs a lengthy listing of files arranged by creation time:ls -lt
- Wildcards will be expanded by the shell to match filenames in the current directory. For example, to display a directory listing of files named
anything.sh
, type the following:ls -l *.sh
- Standard input (stdin) and standard output (stdout) are concepts that are followed by the majority of Linux commands and programs. The program receives a stream of data as stdin and produces a stream of output as stdout. These are frequently both connected to Terminal so that input is made via the keyboard and output is displayed on the screen. You can reroute stdin and stdout using the shell.
cat
is an abbreviation for concatenate. When run, the following command will show you the contents of one or more files without requiring you to open them for editing:cat /etc/passwd
- The shell has the ability to pipe data from one program’s output to another’s input.
|
is the pipe symbol. To count the number of words intestfile.txt
, we can concatenate the file and pass the output into thewc
program, like so:cat testfile.txt | wc -w
1198
Or, to count the number of lines from a testfile.txt
file, we can use the following command:
cat testfile.txt | wc -l 289
- You can create aliases for commands or groups of commands that you use frequently or find difficult to input. For instance, we could use the
top10
alias to find the top 10 files in the current directory.head
will show only the top lines. An alias is a shortcut for a command—for example, rather than remembering a very long command, you can create an alias that you can remember easily. Here’s an example:alias top10="du -hsx * | sort -rh | head -10"
- Some variables are predefined, such as
$HOME
, which is yourhome
directory. To see a list of assigned variables, type the following command:set
- A manual (man) page is like a manual with instructions and descriptions about each command. Run the following command to view the man page for Bash:
bash-3.2$ man bash
- Scripts of shell commands can be written. These can be called just like compiled programs (that is, just by naming them). For instance, we first create a file in
/bin
containing the following in order to construct a script namedtop10.sh
that displays the top 10 biggest files in the current directory:#! /bin/bash
du -hsx * | sort -rh | head -10
- We must next use the
chmod
command to make the file executable before we can run it normally:chmod +x ~/bin/top10.sh
./top10.sh
See the man page on bash
for more details (type man bash
).
The up arrow key on the keyboard in Bash’s extra mechanism enables you to access and modify past commands. The most recent command is displayed on Terminal again when you press the up arrow key. To access previous commands, press the up arrow key once more. Press Enter to run the command once more. Use the Delete key to remove characters from the command’s end, or the back arrow key to move the cursor and change the command’s contents by inserting or deleting characters.
By using the history
command, you can view the history of commands.
You can rerun any command from the history by pressing !
and the line number—for example, !345
.
Now that you know how to interact with the shell and what is happening when you type these commands, in the next section, we will try to practice some basic commands to make you more confident when you interact with Terminal.