Using the Bash Shell on Ubuntu 11.04

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Configuring the Ubuntu 11.04 Unity File ManagerUbuntu 11.04 Unity Desktop - Starting Applications on Login


Purchase and download the full version of this eBook in PDF & ePub formats for only $12.99
PDF/ePub version contains 39 chapters and 255 pages.

Buy eBook


An important part of learning to work with Ubuntu, and Linux distributions in general, involves gaining proficiency in working in the shell environment. Whilst the graphical desktop environments such as Unity, GNOME and KDE 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 Ubuntu 11.04 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 Ubuntu 11.04 (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. 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 Bourne shell and the C shell. The default shell on Ubuntu 11.04 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 Ubuntu Unity desktop environment, the shell prompt may be accessed by pressing Ctrl-Alt-T or searching for Terminal in the Applications dash. When remotely logging into an Ubuntu system, for example using SSH, the user is also presented with a shell prompt. 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 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. Whilst 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:

[email protected]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

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:

which ls
/bin/ls

Clearly the ls command resides in the /bin directory.

Using the sudo Command

A typical Ubuntu system will have a number of commands and file systems areas that are inaccessible by default to general users of the system. The reason for this is to prevent users from accidentally damaging the system. In order to perform such tasks the user is required to gain super user or root privileges. On Ubuntu this is achieved by prefixing the command with sudo. For example:

sudo apt-get update

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 man command specifying the name of the command as an argument. For example, to learn more about the pwd command:

man pwd

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:

wc --help

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:

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

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:

[email protected]$ history
    1  ps
    2  ls
    3  ls –l /
    4  ls
    5  man pwd
    6  man apropos

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:

cat list.txt

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

cat list.txt list2.txt list3.txt list4.txt

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 to can be used to simplify the above example:

cat *.txt

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:

cat list*.txt

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

cat list?.txt

File Name 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 a feature called file name completion. 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 stdout which is essentially the terminal window or console in which the shell is running. Conversely, the shell takes input from a device file named 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 ‘>’ character. For example, to redirect the output from an ls command to a file named files.txt, the following command would be required:

ls *.txt > files.txt

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:

wc –l < files.txt

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

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

ls *.dat >> files.txt

In addition to standard output, the shell also provides standard error output using stderr. Whilst 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> operator:

ls dkjfnvkjdnf 2> errormsg

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 &> operator:

ls /etc dkjfnvkjdnf &> alloutput

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 process running on a system the output from the ps command can be piped through to the wc command:

ps –ef | wc –l

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

cat namesfile | grep Smith | wc –l

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:

ls –lt

To reduce the amount of typing involved in issuing a command it is possible to create an alias that maps to the command and the 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:

alias l=’ls –lt’

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:

[[email protected] ~]$ env
HOSTNAME=rhel6
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.2.18 50317 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/1
USER=demo
.
.
.

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 newly installed Ubuntu 11.04 system running a Unity desktop session will likely be configured as follows:

[email protected]:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

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:

export PATH=$PATH:$HOME/scripts

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

echo $PATH

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

export DATAPATH=/data/files

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:

export NOW=`date`
echo $NOW
Wed Jun 15 10:07:05 EDT 2011

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:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
export DATAPATH=/data/files
alias l='ls -lt'

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 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:

#!/bin/sh

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:

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

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:

sh simple.sh

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

chmod +x simple.sh

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

./simple.sh

Summary

In this chapter of Ubuntu 11.04 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 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.


Purchase and download the full version of this eBook in PDF & ePub formats for only $12.99
PDF/ePub version contains 39 chapters and 255 pages.

Buy eBook



PreviousTable of ContentsNext
Configuring the Ubuntu 11.04 Unity File ManagerUbuntu 11.04 Unity Desktop - Starting Applications on Login