Module_5a_Bash_Shell

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

INFO-3182- Operating systems - Linux

Module 5a Bash Shell


Introduction

Linux administrators spend much of their time using a terminal interface. This may be a physical
terminal, a terminal window via a graphical desktop, or a remote connection. No matter how the
administrator is connected to the system, the program running in the terminal is known as a shell.

This document discusses some of the shell programs available and then goes into the use of the bash
shell in some detail. Methods for obtaining help are also discussed.

Getting help.

There are several ways to get help in a Linux environment: manual (man) pages, info pages, readme and
example files, on-line documentation, and the help tool for graphical applications.

Manual pages, normally referred to as man pages, are the original form of Linux documentation and
consist of an extensive set of formatted documents on programs and their configuration files. Man
pages are divided into sections (represented by directories on the file system) as listed below (see man
man, the man page for man):

1) Executable programs or shell commands


2) System calls (functions provided by the kernel)
3) Library calls (functions within program libraries)
4) Special files (usually found in /dev)
5) File formats and conventions, e.g. /etc/passwd
6) Games
7) Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8) System administration commands (usually only for root)
9) Kernel routines [Non standard]

Sections 1, 5 and 8 are used by Linux administrators more than any of the other sections. In most cases,
the section can be ignored, it is only necessary to specify the section number if there are topics in
multiple sections with the same name

For example, man ls or man 1 ls displays the man page for the ls command from section 1 and
man fstab or man 5 fstab provides a description of the structure of the /etc/fstab file from
section 5. The section number is shown at the top left of the first page of each man page.

Page 1 of 13
INFO-3182- Operating systems - Linux

The man command produces output that is scrollable and searchable. The scroll and search functionality
is like that for the vim editor (see the next Module).

Figure 1 Screenshot showing the output of the man man command. This is the documentation for the man command displayed
using the man command itself.

The GNU info tool was developed as an alternative to man, but to date it mostly contains the
documentation for the GNU software projects. Most things that are included in info are also in man.

Many software developers include example configuration files, readme files and possibly other
documentation for their software. This information is usually located in software specific subdirectories
of /usr/share/doc.

Linux systems with GUI desktops usually include a graphical tool provided by the desktop developers
with information on graphical-only tools.

Websites containing information on Linux systems and tools are plentiful. Use the project development
team’s web sites and man page sites that are regularly updated as much as possible, there are a lot of
sites out there with incorrect information on them.

Page 2 of 13
INFO-3182- Operating systems - Linux

Shell programs

A shell program (or just shell) is a program that permits a user to type a command for the system to
interpret and execute. Shell programs are often referred to as command interpreters. Before attempting
to execute a command, the shell checks it for correctness from the shell’s point of view, and when
appropriate, adjusts the structure of the command.

Commands can be either internal or external. An internal command is built into the shell. External
commands are executable files External commands are programs in an executable binary format or
interpretable script files. Executable binary programs are crated using programming languages such as C,
C++, Fortran, etc. Interpretable script files are written in languages such as PHP, Perl, Python or the
scripting capabilities of the shell being used.

Over the years there have been numerous shells for Unix and then Linux, such as:

• tsh – The Thompson shell is the original Unix shell. It was written by Ken Thompson. This shell
is still available for some Unix and Linux distributions but is seldom used because of the added
features of newer shells.
• csh – The C shell, this is one of the oldest shells and is not widely used anymore, again, because
of the features available in newer shells.
• sh – The Bourne shell, for years the most popular shell for Unix and Linux systems. It is the basis
for many of the newer shells. This shell is still installed on many Linux systems, including CentOS
Stream 9 and Ubuntu 24.04. On some modern Linux systems, sh is a symbolic link to bash,
instead of being a separate program.
• bash – The Bourne again shell is currently the most popular shell on Linux systems. New user
accounts on CentOS Stream 9 are assigned this shell by default. On Ubuntu 24.04, bash is used
as the default shell for new users in some cases and sh in others (discussed later in the course).
• rbash – A restricted version of bash. Same as using the -r parameter for bash.
• dash – A newer shell designed to adhere to the POSIX (Portable Operating System Interface)
standard. Ubuntu 24.04 includes this shell by default, but CentOS Stream 9 does not.
• nologin – This is a special shell that prevents a user from logging in interactively. This shell is
used in situations where a user needs to log in via a service but is not to be allowed to log in
interactively.

NOTE: The bash shell can be made POSIX compliant by using the --posix parameter.

The bash shell

As noted above the bash shell is the most widely used shell for Linux systems. It is the default shell for
users created during CentOS Stream 9 and Ubuntu 24.04 system installation. It is also the default shell
for Mac OS systems and is available for newer Windows systems.

Page 3 of 13
INFO-3182- Operating systems - Linux

When a Linux user logs into a system without a graphical desktop or opens a terminal window on a
system with a graphical desktop, they are presented with a shell. The shell each user is presented with is
set in their user account. In most cases, this is the bash shell.

The primary purpose of bash (and all other shells) is to interpret the commands a user types and then
have the operating system execute the command. This may be as simple as recognizing that the user has
entered the name of a program and pressed enter without providing any other information, to a
complex command which includes actions to be performed by the shell before the command can be
executed. For example, consider these two commands that do directory listings, assuming that the user
is in their home directory.
ls
ls ~

Figure 2 Screenshot showing the ls and ls ~ commands and their output.

Figure 2 shows the execution of both commands in the current user’s home directory (/home/thall). The
output of both commands looks (and is) the same. But the execution of the commands is significantly
different. In the first command (ls), no location for the directory listing is given so the current directory
is used. The ls command automatically uses the current directory if no location is specified as a
parameter for it. In the second command (ls ~), the location to do the directory listing of is given as ~.
The bash interpreter uses ~ as an abbreviation for the current user’s home directory and expands
(converts) this abbreviation to the fully qualified path for the current user’s home directory (/home/thall
in the example). This means that the command entered is ls ~bash executes:
ls /home/thall

This is an example of one type of what is known as command expansion (specifically Tilde expansion)
and demonstrates some of the power of bash.

Environment variables (pre-defined variables containing information about the current state of a user’s
environment) are used extensively by Linux shells. One environment variable that is defined for all users
is HOME which contains the fully qualified path of that user’s home directory. A third way to list the
contents of a user’s home directory is to use this variable with the ls command.

Page 4 of 13
INFO-3182- Operating systems - Linux

ls $HOME

which is executed as
ls /home/thall

Note that while the variables name is HOME, it must be referred to as $HOME to retrieve the variable’s
value, otherwise bash will treat it as a simple string, not a variable. This is another type of command
expansion (Parameter and variable expansion).

As can be seen from the three examples, above, bash is a flexible and powerful tool. Some of its features
are used only interactively (e. g. ~) wile others are normally used in scripts, but many are used
interactively and in scripts. This module looks at the features of bash that can be used interactively.

Environment variables

The previous section mentioned the HOME environment variable. Environment variables are variables
defined within a shell’s process that define the operating environment for that process. There are many
environment variables that are automatically created when a user logs in to a system and/or when a
graphical terminal session is started.

Each Linux distribution and many software packages define the environment variables needed by users.

By convention, the letters in an environment variable’s name are always uppercase. For example, two
environment variables that are created for every user are PATH and HOME. The PATH environment
variable contains the program search path (a colon-separated list of directory names). As noted above,
the HOME environment variable contains the name of a user’s home directory.

To display the environment variables available in a specific shell session, use the built-in command:
set

without any parameters. The output of this command is long and is often piped to the grep command
to select the desired variables.

Methods for users to work with environment variables and other types of variables are discussed later in
the course.

Start up files

Linux systems use special script files called start up files to set up the environment variables and other
items needed for a user to work properly when the user logs in. The number of start up files and their
names vary from one Linux distribution to another. Start up files are script files written using the

Page 5 of 13
INFO-3182- Operating systems - Linux

scripting capabilities of the shell being used and are executed automatically when a user logs in and/or
starts a graphical terminal session.

On a CentOS Stream 9 system, the start up files include:

• /etc/profile - Always executed, contains the definition of critical data, e. g. the PATH and
HOME environment variables. This file is created during system installation and may be
modified by the system update process. System administrators can modify this file, but
changes may be lost during system updates.
• /etc/profile.d/*.sh – Always executed, contains additional start up definitions based
on the software installed on the system. System administrators can also add local system-wide
configuration in their own files in this directory.
• ~/.bash_profile – Always called, this is the first user specific startup file. By default, the
only thing done is to execute ~/.bashrc if it exists. Many user customizations are added at the
end of this file, so that they are not overridden by any other startup file settings. The default
contents of this file are shown in Figure 3.

Figure 3 Screenshot of the default contents of the ~/.bash_profile file.

• ~/.bashrc – This file contains a call to the /etc/bashrc startup script if it exists, followed by
environment setup commands that are specific to individual users.
• /etc/bashrc – This file contains more global environment setup commands. This file may
be updated by the system update process.

On Ubuntu 24.04 the startup files are:

• /etc/profile - Always executed, contains the definition of many environment variables.


This file is created during system installation and may be modified by the system update
process. System administrators can modify this file, but changes may be lost during system
updates.

Page 6 of 13
INFO-3182- Operating systems - Linux

• /etc/profile.d/*.sh – Always executed, contains additional start up definitions based


on the software installed on the system. System administrators can also add local system-wide
configuration in their own files in this directory.
• ~/.profile – Always executed, this file calls ~/.bashrc if it exists . After the call to ~/.bashrc,
some user-specific settings are defined, including the PATH and HOME environment variables.
Custom settings for individual user’s are put at the end of this file.
• ~/.bashrc – Contains many user settings. Ubuntu 24.04 does not have a /etc/bashrc file.

Figure 4 Screenshot showing the contents of the ~/.profile file on an Ubuntu 24.04 system.

There is also a file named ~/.bash_logout which as its name implies is used when the user logs out
of the system. On CentOS Stream 9 systems, this file contains only a comment by default. On Ubuntu
24.04 systems, it contains a command to clear the screen if the user was logged onto the system
console. Additional commands can be added to perform any necessary cleanup as a user logs out.

Page 7 of 13
INFO-3182- Operating systems - Linux

Aliases

Users often call the same command repeatedly, to allow these commands to be run without so much
typing, bash includes the alias built-in command. The alias command allows a user or an
administrator to define an alternate name (alias) for the commands being reused frequently. Individual
users can define their own aliases while administrators can define their own aliases or system-wide
aliases.

Using the alias command interactively creates a temporary alias that exists only within the shell
session in which it is defined. Adding alias commands to the end of either the ~/.bash_profile or
~/.profile file (see operating system specifics above) creates aliases that are always available to any shell
the user opens. For system-wide aliases, an administrator needs to add their definitions to a custom file
in /etc/profile.d.

Many Linux systems have a few aliases that are provided by the Linux distribution being used. For
example, listed below are some of the pre-defined aliases provided with CentOS Stream 9:
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'

The first and last of these aliases turn on coloring functionality for the grep and ls command
respectively. The middle two provide shortcuts for running two common ls commands, including
coloration.

As an example of defining a alias. When working with the Kubernetes container orchestration system,
the widely used command line tool for performing most management operations is kubectl followed
by the parameters necessary to perform the desired operation, but when using Canonical’s MicroK8S
implementation of Kubernetes, the command is microk8s kubectl. Many Kubernetes
administrators find this confusing, especially if they need to switch between multiple Kubernetes
platforms. The best way to eliminate this confusion is to add an alias on the system running MicroK8S.
The command to do this is:
alias kubectl=’microk8s kubectl’

CAUTION: If an alias and a program have the same name, the alias will take precedence over the
program from the user’s perspective. Aliases themselves are designed to avoid this problem, so the
previous examples for grep and ls work properly. In this last example, if the ‘real’ kubectl program is
installed on the system, along with MicroK8S, the real program will not be accessible by the user with
the alias unless its fully qualified name is used.

IMPORTANT: Aliases only work for interactive sessions. They are not available in scripts.

Page 8 of 13
INFO-3182- Operating systems - Linux

WARNING: Be careful, using alias names that are the same as command names may result in
unexpected and possibly disastrous results. Always check what you are defining thoroughly before
implementing it. For example, consider the following poorly designed aliases:

• alias dd=‘rm -f’ - use the vim delete line command as an alias for forcibly removing
files.
• alias copydisk=‘dd’ – use copydisk as an alias for the disk duplication command.

When copydisk is used, it will be mapped to dd, in turn dd will be mapped to rm -f, meaning
that instead of copying the contents of one disk partition to another using their device file names, the
device files will be deleted.

Command history

The bash shell includes a history feature for keeping track of recently used commands. This is not a
logging feature. By default, the last 1000 commands are kept in a cache and written to disk when the
user logs out or the cache exceeds 1000 entries. Each user has their own cache and disk file (the file is
~/.bash_history).

The ~/.bash_history file is a plain text file that contains commands in the order they were executed,
once they have been written from cache. When this file is full, the oldest commands are discarded.

Several environment variables affect how and where the history is stored, they are (CentOS Stream
default values shown):

• HISTCONTROL=ignoredups – Allow the stored history to ignore duplicate commands (the


same command, including parameters, executed two or more times in a row). There are several
other values available for this variable that can be used individually and in conjunction with one
another.

• HISTFILE=/home/thall/.bash_history – The location of the history file. This value is


set automatically, and no attempt should be made to change it.
• HISTFILESIZE=1000 – The maximum number of commands that can be stored in the
history file.
• HISTSIZE=1000 – The maximum number of commands that can be cached at any given time.

It is seldom necessary to change these environment variables from their default values.

There are several ways to access commands that are in a user’s history when using the bash shell,
including:

• Using the up and down arrow keys to scroll through history. The up arrow goes back to earlier
commands and the down arrow key moves to more recent commands. A user must start using

Page 9 of 13
INFO-3182- Operating systems - Linux

this method by using the up-arrow Key This method uses the functionality of the readline
library.
• history built-in command
• fc – The built-in fix command.
• Text searches of the contents of the ~/.bash_history file.

On most modern systems that have keyboards with arrow keys, the first and last of these methods are
usually employed. See the man page for bash for details on using history and fc, if they are
needed. Since these two commands are built into bash, they do not have their own man pages.

Tab completion

Bash supports tab completion in several ways:

• Typing part of a command name and pressing TAB once will complete the name of the
command if the part of the name typed is sufficient to uniquely identify a single command.
• OR if the part of the command name entered does not uniquely identify a single command
pressing TAB a second time will list all commands that match the part of the command name
typed. The user can then complete the command name using the listed commands as a guide.

Figure 5 Screenshot showing TAB being pressed twice after entering ss. A list of available command beginning with ss
is output.

• Typing part of a parameter name or value and pressing enter will complete the parameter or
value if it can be uniquely identified. This is especially useful for file or directory name
completion.

Quoted strings

Bash, like many other tools, treats strings enclosed in single and double quotes differently.

Page 10 of 13
INFO-3182- Operating systems - Linux

o Strings enclosed in single quotes are treated literally. Every character in the string is assumed to
have its ‘normal’ meaning with very few exceptions.
o Characters that have special meanings in strings enclosed in double quotes retain their special
meanings unless escaped (usually by the \ character).

For example, Figure 6 shows two executions of the echo command, the first with double quotes
enclosing the string and the second with single quotes. In the first case, the $(whoami) is replaced with
the name of the user executing the command using command substitution (see below). The second
command includes the literal $(whoami) because the single quotes prevent command substitution.

Figure 6 Screenshot showing the different behaviour of strings depending on the quotes used.

Command line processing

As stated earlier, a shell checks and interprets each command it receives before executing it This
checking and interpreting takes place in a specific order that ensures the command will receive
parameters that it can process properly. If the shell detects any problems, it will not execute the
command. The shell can only ensure that the command exists, and that the data passed to it is properly
formatted, it cannot check the validity of the data passed to the command. Formatting the data may
involve the substitution of the results of a command or arithmetic operations for the command or
operation that was part of the original command line.

For the bash shell , the checking and interpretation is done in several steps as listed below.

• Command history processing (usually interactive).


Bash retrieves any commands from history as required by the command being interpreted and
substitutes the retrieved command for the history operations in the command being
interpreted.
• Alias processing (interactive only).
Bash removes any aliases from the command being interpreted and replaces them with the
command (and parameters) referred to by the alias.
• Command expansion
o Brace expansion (interactive or script)

Page 11 of 13
INFO-3182- Operating systems - Linux

Bash uses braces {} to denote a set of values that are to be used to complete something
else. For example:
touch file{a,b,c,d}.txt

will create four files, filea.txt, fileb.txt, filec.txt, and filed.txt., and
touch file{1..4}.txt

will create four files, file1.txt, file2.txt, file3.txt, and file4.txt.


Brace expansion generates the full list of items (files in these examples) from the
contents of the braces.
o Tilde expansion (usually interactive)
Bash substitutes the fully qualified path of the current user’s home directory for the ~
character. While this works in scripts as well as interactively, avoid using ~ in scripts to
mitigate problems that often arise when a different user runs a script.
o Parameter and variable expansion (interactive or script)
Bash substitutes the value of variable or predefined constants for their names. See the
ls example above.
o Arithmetic expansion (interactive or script)
Bash performs any arithmetic operations and substitutes the result for the arithmetic
expression. For example:
echo $((5 + 3))

Becomes
echo 8

o Command substitution (interactive or script)


Bash executes any nested commands and substitutes the output of the command for
the command. For example:
echo My username is $(whoami)

becomes
echo My username is thall

when the current user’s username is thall.


o Word splitting (interactive or script).
Arithmetic expansions and command substitutions often produce lists of values, word
splitting ensures that the list items are properly separated, based on the value of the IFS
environment variable.
o Pathname expansion (interactive or script).
Bash supports the use of some special characters in file and directory names, such as:
 . (period) – The current directory.
 ? – Match any single character in a file or directory name.
 * - Match zero (0) or more characters in a file or directory name.

Page 12 of 13
INFO-3182- Operating systems - Linux

These characters usually need to be replaced with the full names of files or directories
before a command can process them.
o Quote removal (interactive or script).
Bash restructures all command parameters to remove any quote symbols that mark the
beginning and end of a single value, otherwise the command will accept these quotation
marks as part of the parameter value.

Page 13 of 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy