Changes

Using the Bash Shell on RHEL

18,766 bytes added, 19:19, 9 June 2019
Created page with "{{#pagetitle: Using the Bash Shell on RHEL 8 }} <table border="0" cellspacing="0" width="100%"><tr> <td width="20%">[[|Previous]]<td align="center">Red Hat Enterprise Linux..."
{{#pagetitle: Using the Bash Shell on RHEL 8 }}
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right">[[Next]]</td>
<tr>
<td width="20%"><td align="center"><td width="20%" align="right"></td>
</table>
<hr>


<htmlet>rhel8</htmlet>


An important part of learning to work with RHEL 8, and Linux distributions in general, involves gaining proficiency in working in the shell environment. While the graphical desktop environments such as GNOME included with Linux provide a user friendly interface to the operating system, in practice the shell environment provides far greater capabilities, flexibility and automation than can ever be achieved using graphical desktop tools. The shell environment also provides a means for interacting with the operating system when a desktop environment is not available; a common occurrence when working with a server-based operating system such as RHEL 8 or a damaged system that will not fully boot.

The goal of this chapter, therefore, is to provide an overview of the default shell environment on RHEL 8 (specifically the Bash shell).

== What is a Shell? ==

The shell is an interactive command interpreter environment within which commands may be typed at a prompt or entered into a file in the form of a script and executed. The origins of the shell can be traced back to the early days of the UNIX operating system. In fact, in the early days of Linux before the introduction of graphical desktops the shell was the only way for a user to interact with the operating system.

A variety of shell environments have been developed over the years. The first widely used shell was the Bourne shell, written by Stephen Bourne at Bell Labs.

Yet another early creation was the C shell which shared some syntax similarities with the C Programming Language and introduced usability enhancements such as command-line editing and history.

The Korn shell (developed by David Korn at Bell Labs) is based on features provided by both the C shell.

The default shell on RHEL 8 is the Bash shell (shorthand for Bourne Again SHell). This shell, which began life as an open source version of the Bourne shell, was developed for the GNU Project by Brian Fox and is based on features provided by both the Bourne shell and the C shell.

== Gaining Access to the Shell ==


From within the GNOME desktop environment, the shell prompt may be accessed from a Terminal window by selecting the Activities option in the top bar, entering Terminal into the search bar and clicking on the Terminal icon.

When remotely logging into a RHEL 8 server, for example using SSH, the user is also presented with a shell prompt. Details on accessing a remote server using SSH will be covered in the chapter entitled ''“Configuring SSH Key-based Authentication on RHEL 8”''. When booting a server-based system in which a desktop environment has not been installed, the shell is entered immediately after the user completes the login procedure at the physical console terminal or remote login session.

== Entering Commands at the Prompt ==

Commands are entered at the shell command prompt simply by typing the command and pressing the Enter key. While some commands perform tasks silently, most will display some form of output before returning to the prompt. For example, the ''ls'' command can be used to display the files and directories in the current working directory:

<pre>
$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
</pre>

The available commands are either built into the shell itself, or reside on the physical file system. The location on the file system of a command may be identified using the ''which'' command. For example, to find out where the ''ls'' executable resides on the file system:

<pre>
$ which ls
alias ls=’ls --color=auto’
&#9;/usr/bin/ls
</pre>

Clearly the ''ls'' command resides in the ''/usr/bin'' directory. Note also that an alias is configured, a topic which will be covered later in this chapter. Using the ''which ''command to locate the path to commands that are built into the shell will result in a message indicating the executable cannot be found. For example, attempting to find the location of the ''history ''command (which is actually built into the shell rather than existing as an executable on the file system) will result in output similar to the following:

<pre>
$ which history
/usr/bin/which: no history in (/home/demo/.local/bin:/home/demo/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)
</pre>

== Getting Information about a Command ==

Many of the commands available to the Linux shell can seem cryptic to begin with. To find out detailed information about what a command does and how to use it, use the ''pwd'' command:

<pre>
$ man pwd
</pre>

When the above command is executed, a detailed description of the ''pwd'' command will be displayed. Many commands will also provide additional information when run with the ''--help'' command-line option:

<pre>
$ wc --help
</pre>

== Bash Command-line Editing ==

Early shell environments did not provide any form of line editing capabilities. This meant that if you spotted an error at the beginning of a long command-line you were typing, you had to delete all the following characters, correct the error and then re-enter the remainder of the command. Fortunately Bash provides a wide range of command-line editing options as outlined in the following table:


<table>
<tr>
<td>Key Sequence</td><td>Action</td>
</tr>
<tr>
<td>Ctrl-b or Left Arrow</td><td> Move cursor back one position</td>
</tr>
<tr>
<td>Ctrl-f or Right Arrow</td><td> Move cursor forward on position</td>
</tr>
<tr>
<td>Delete</td><td> Delete character currently beneath the cursor</td>
</tr>
<tr>
<td>Backspace</td><td> Delete character to the left of the cursor</td>
</tr>
<tr>
<td>Ctrl-_</td><td> Undo previous change (can be repeated to undo all previous changes)</td>
</tr>
<tr>
<td>Ctrl-a</td><td> Move cursor to the start of the line</td>
</tr>
<tr>
<td>Ctrl-e</td><td> Move cursor to the end of the line</td>
</tr>
<tr>
<td>Meta-f or Esc then f</td><td> Move cursor forward one word</td>
</tr>
<tr>
<td>Meta-b or Esc then b</td><td> Move cursor back on word</td>
</tr>
<tr>
<td>Ctrl-l</td><td> Clear the screen of everything except current command</td>
</tr>
<tr>
<td>Ctrl-k</td><td> Delete to end of line from current cursor position</td>
</tr>
<tr>
<td>Meta-d or Esc then d</td><td> Delete to end of current word</td>
</tr>
<tr>
<td>Meta-DEL or Esc then DEL</td><td> Delete beginning to current word</td>
</tr>
<tr>
<td>Ctrl-w</td><td> Delete from current cursor position to previous white space</td>
</tr>
</table>

== Working with the Shell History ==

In addition to command-line editing features, the Bash shell also provides command-line history support. A list of previously executed commands may be viewed using the ''history'' command:

<pre>
$ history
1 ps
2 ls
3 ls –l /
4 ls
5 man pwd
6 man apropos
</pre>

In addition, Ctrl-p (or up arrow) and Ctrl-n (or down arrow) may be used to scroll back and forth through previously entered commands. When the desired command from the history is displayed, press the Enter key to execute it.

Another option is to enter the ‘!’ character followed by the first few characters of the command to be repeated followed by the Enter key.

== Filename Shorthand ==

Many shell commands take one or more filenames as arguments. For example, to display the content of a text file named ''list.txt'', the ''cat'' command would be used as follows:

<pre>
$ cat list.txt
</pre>

Similarly, the content of multiple text files could be displayed by specifying all the file names as arguments:

<pre>
$ cat list.txt list2.txt list3.txt list4.txt
</pre>

Instead of typing in each name, pattern matching can be used to specify all files with names matching certain criteria. For example, the ‘*’ wildcard character can be used to simplify the above example:

<pre>
$ cat *.txt
</pre>

The above command will display the content of all files ending with a ''.txt'' extension. This could be further restricted to any file names beginning with ''list'' and ending in ''.txt'':

<pre>
$ cat list*.txt
</pre>

Single character matches may be specified using the ‘?’ character:

<pre>
$ cat list?.txt
</pre>

== Filename and Path Completion ==

Rather than typing in an entire file name or path, or using pattern matching to reduce the amount of typing, the shell provides the ''filename completion'' feature. In order to use filename completion, simply enter the first few characters of the file or path name and then press the Esc key twice. The shell will then complete the filename for you with the first file or path name in the directory that matches the characters you entered. To obtain a list of possible matches, press Esc = after entering the first few characters.

== Input and Output Redirection ==

As previously mentioned, many shell commands output information when executed. By default this output goes to a device file called ''stdin'', which by default is the keyboard.

Output from a command can be redirected from stdout to a physical file on the file system using the ‘&gt;’ character. For example, to redirect the output from an ''ls'' command to a file named ''files.txt'', the following command would be required:

<pre>
$ ls *.txt &gt; files.txt
</pre>

Upon completion, ''files.txt'' will contain the list of files in the current directory. Similarly, the contents of a file may be fed into a command in place of stdin. For example, to redirect the contents of a file as input to a command:

<pre>
$ wc –l &lt; files.txt
</pre>

The above command will display the number of lines contained in the ''files.txt'' file.

It is important to note that the ‘&gt;’ redirection operator creates a new file, or truncates an existing file when used. In order to append to an existing file, use the ‘&gt;&gt;’ operator:

<pre>
$ ls *.dat &gt;&gt; files.txt
</pre>

In addition to standard output, the shell also provides standard error output using ''stderr''. While output from a command is directed to stdout, any error messages generated by the command are directed to stderr. This means that if stdout is directed to a file, error messages will still appear in the terminal. This is generally the desired behavior, though stderr may also be redirected if desired using the ‘2&gt;’ operator:

<pre>
$ ls dkjfnvkjdnf 2&gt; errormsg
</pre>

On completion of the command, an error reporting the fact that the file named ''dkjfnvkjdnf'' could not be found will be contained in the ''errormsg'' file.

Both stderr and stdout may be redirected to the same file using the &amp;&gt; operator:

<pre>
$ ls /etc dkjfnvkjdnf &amp;&gt; alloutput
</pre>

On completion of execution, the ''alloutput'' file will contain both a listing of the contents of the'' /etc'' directory, and the error message associated with the attempt to list a non-existent file.

== Working with Pipes in the Bash Shell ==

In addition to I/O redirection, the shell also allows output from one command to be piped directly as input to another command. A pipe operation is achieved by placing the ‘|’ character between two or more commands on a command-line. For example, to count the number of processes running on a system, the output from the ''wc'' command:

<pre>
$ ps –ef | wc –l
</pre>

There is no limit to the number of pipe operations that can be performed on a command-line. For example, to find the number of lines in a file which contain the name Smith:

<pre>
$ cat namesfile | grep Smith | wc –l
</pre>

== Configuring Aliases ==

As you gain proficiency with the shell environment it is likely that you will find yourself frequently issuing commands with the same arguments. For example, you may often use the ''ls'' command with the ''l'' and ''t'' options:

<pre>
$ ls –lt
</pre>

To reduce the amount of typing involved in issuing a command, it is possible to create an alias that maps to the command and arguments. For example, to create an alias such that entering the letter ''l'' will cause the ''ls –lt ''command to be executed, the following statement would be used:

<pre>
$ alias l=&quot;ls –lt&quot;
</pre>

Entering ''l ''at the command prompt will now execute the original statement.

== Environment Variables ==

Shell environment variables provide temporary storage of data and configuration settings. The shell itself sets up a number of environment variables that may be changed by the user to modify the behavior of the shell. A listing of currently defined variables may be obtained using the ''env'' command:

<pre>
$ env
SSH_CONNECTION=192.168.0.19 61231 192.168.0.28 22
MODULES_RUN_QUARANTINE=LD_LIBRARY_PATH
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=rhel8-pc.ebookfrenzy.com
XDG_SESSION_ID=15
MODULES_CMD=/usr/share/Modules/libexec/modulecmd.tcl
USER=demo
ENV=/usr/share/Modules/init/profile.sh
SELINUX_ROLE_REQUESTED=
PWD=/home/demo
HOME=/home/demo
SSH_CLIENT=192.168.0.19 61231 22
SELINUX_LEVEL_REQUESTED=
.
.
.
</pre>

Perhaps the most useful environment variable is PATH. This defines the directories in which the shell will search for commands entered at the command prompt, and the order in which it will do so. The PATH environment variable for a user account on newly installed RHEL 8 system will likely be configured as follows:

<pre>
$ echo $PATH
/home/demo/.local/bin:/home/demo/bin:/usr/share/Modules/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
</pre>

Another useful variable is HOME which specifies the home directory of the current user. If, for example, you wanted the shell to also look for commands in the scripts directory located in your home directory, you would modify the PATH variable as follows:

<pre>
$ export PATH=$PATH:$HOME/scripts
</pre>

The current value of an existing environment variable may be displayed using the echo command:

<pre>
$ echo $PATH
</pre>

You can create your own environment variables using the ''export'' command. For example:

<pre>
$ export DATAPATH=/data/files
</pre>

A useful trick to assign the output from a command to an environment variable involves the use of back quotes (`) around the command. For example, to assign the current date and time to an environment variable called NOW:

<pre>
$ export NOW=`date`
$ echo $NOW
Tue Apr 2 13:48:40 EDT 2019
</pre>

If there are environment variable or alias settings that you need to be configured each time you enter the shell environment, they may be added to a file in your home directory named ''.bashrc''. For example, the following example ''.bashrc'' file is configured to set up the DATAPATH environment variable and an alias:

<pre>
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
&#160;
# User specific environment
PATH=&quot;$HOME/.local/bin:$HOME/bin:$PATH&quot;
export PATH
&#160;
# Uncomment the following line if you don&apos;t like systemctl&apos;s auto-paging feature:
# export SYSTEMD_PAGER=
&#160;
# User specific aliases and functions
export DATAPATH=/data/files
alias l=&quot;ls -lt&quot;
</pre>

== Writing Shell Scripts ==

So far we have focused exclusively on the interactive nature of the Bash shell. By interactive we mean manually entering commands at the prompt one by one and executing them. In fact, this is only a small part of what the shell is capable of. Arguably one of the most powerful aspects of the shell involves the ability to create shell scripts. Shell scripts are essentially text files containing sequences of statements that can be executed within the shell environment to perform tasks. In addition to the ability to execute commands, the shell provides many of the programming constructs such as ''for'' and ''do'' loops and ''if'' statements that you might reasonably expect to find in a scripting language.

Unfortunately a detailed overview of shell scripting is beyond the scope of this chapter. There are, however, many books and web resources dedicated to shell scripting that do the subject much more justice than we could ever hope to achieve here. In this section, therefore, we will only be providing a very small taste of shell scripting.

The first step in creating a shell script is to create a file (for the purposes of this example we name it ''simple.sh'') and add the following as the first line:

<pre>
#!/bin/sh
</pre>

The #! is called the “shebang” and is a special sequence of characters indicating that the path to the interpreter needed to execute the script is the next item on the line (in this case the ''sh'' executable located in /bin). This could equally be, for example, ''/bin/csh'' or ''/bin/ksh'' if either were the interpreter you wanted to use.

The next step is to write a simple script:

<pre>
#!/bin/sh
for i in *
do
echo $i
done
</pre>

All this script does is iterate through all the files in the current directory and display the name of each file. This may be executed by passing the name of the script through as an argument to ''sh'':

<pre>
$ sh simple.sh
</pre>

In order to make the file executable (thereby negating the need to pass it through to the ''chmod ''command can be used:

<pre>
$ chmod +x simple.sh
</pre>

Once the execute bit has been set on the file’s permissions, it may be executed directly. For example:

<pre>
$ ./simple.sh
</pre>

== Summary ==

In this chapter of RHEL 8 Essentials we have taken a brief tour of the Bash shell environment. In the world of graphical desktop environments it is easy to forget that the true power and flexibility of an operating system can often only be utilized by dropping down below the user friendly desktop interface and using a shell environment. Moreover, familiarity with the shell is a necessity when required to administer and maintain server-based systems that do not have the desktop installed or when attempting to repair a system that is damaged to the point that the desktop or Cockpit interface will no longer launch.

The capabilities of the shell go far beyond the areas covered in this chapter. If you are new to the shell then we strongly encourage you to seek out additional resources. Once familiar with the concepts you will quickly find that it is quicker to perform many tasks using the shell in a terminal window than it is to wade through menus and dialogs on the desktop.