Linux+ TechNotes
Bash and the Shell Environment

 

Index
Directory Navigation
Creating and editing files using VI
Bash Scripting


A shell is nothing more than a program that allows a user to input commands to be processed by an operating system kernel. In Windows the shell can refer to either the DOS-based command shell or the Explorer GUI interface. In the Unix world, the distinction between the shell and the kernel is even more apparent. There are dozens of different shells that offer different features but each is interchangeable. The default shell in Linux is the bash shell.

Bash is an improvement on the original Unix Bourne shell (abbreviated sh) and contains features such as command history, tab completion and simplified I/O redirection. Bash contains a number of basic commands for controlling the operating system. These builtin commands include cd, echo, kill and pwd as well as several others. Other common Linux shells are the Korn shell (ksh) and the C shell (csh).

When a user account is created, a shell can be assigned to it. This shell is launched when the user logs in and provides a command prompt for the user to work with. Most shells also will look for specific files to execute when the user logs in. The bash shell looks for the files .bash_profile, .bash_login and .bashrc in the user’s home directory. These files can contain applications to launch, maintenance routines to be performed, or custom environment settings.

The term environment is used to describe the collection of settings that are used to customize the way the operating system works. An environmentvariable defines a single setting. There are dozens of predefined environment variables. You can view all currently defined environment variables with the env or set commands. Some of the more commonly modified environment variables are listed below:

Variable Name Description
DISPLAY Defines which screen to direct the output of the X Windows server
PATH A list of directories to search for an executable file if a full pathname is not provided
PS1 Defines the format of the Bash prompt
TERM Defines the type of terminal emulation to use

You can define your own environment variables for use with custom applications by specifying the variable name, an equals sign, and the value to assign. For example:

KEYBOARD=en_us

An environment variable assigned in this way will only be seen by the current bash shell. Other applications will not be aware of it unless you export it:

export KEYBOARD

Even then, an environment variable only exists as long as you are logged in. The next time you login you will have to set the variable again, or add it to one of your login scripts instead.


Directory Navigation

As most commonly used operating systems, Linux organizes its files in a hierarchical tree structure. The / directory is the highest level of the tree, also known as the root. It is critical for anyone using Linux to be able to navigate the directory structure. There are two primary commands: pwd which displays the current working directory, and cd, used to change directories. The cd command can be used to specify an absolutepathname. For example:

cd /usr/local

This will make /usr/local your current directory. cd can also be used with relativepathnames:

[root@server]# pwd
/usr
[root@server]# cd local
[root@server]# pwd
/usr/local


Linux recognizes a number of characters with special meanings. Three of these special characters relate to directory navigation.

Character Meaning
. The current directory
.. The parent directory
~ The user’s home directory

Suppose the user ‘mjohnson’ logs into a server with the following directory structure and changes the current working directory to /usr/local:



To make /home/mjohnson the current working directory, the user could type any of the following commands:

cd ~
cd /home/mjohnson
cd ../../home/mjohnson


Assuming your user login has appropriate permissions, you can create a new directory using the mkdir command. Simply specify the absolute or relative name of the directory to create:

mkdir /etc/firewall

A directory can be deleted using the rmdir command. Creating a file is done through a specific application depending on the type of file. A text file is created using a text editor such as vi. To create a new file, simply type vi then the name of the file to create. For example:

vi testfile

You can create a link to a file using the ln command. Ln can be used to create two different types of links: hardlinks and symboliclinks. Creating a hard link results in two logical files that both reference a single section of data. The hard link becomes indistinguishable from the original file. If either the original file or the hard link is deleted, the section of data is still accessible from the remaining logical file.



A symbolic or soft link is equivalent to a shortcut in Windows. The symbolic link points to the original file. If the original file is deleted, the link is broken and becomes useless. The ln command creates hard links by default. Symbolic links can be created by using the –s switch.




Creating and editing files using vi

Vi is the most commonly used text editor in the Unix world. It is command line driven and not very user friendly but it is found in virtually every flavor of Unix or Linux and is extremely powerful given a little practice.

The vi editor has three modes of operation:

Command mode - This is the default mode of vi.  Keystrokes entered while in command mode are treated as commands.  There is no prompt.

Insert mode – When in Command mode, pressing a lowercase i enters Insert mode.  This allows you to type and delete text.  Insert mode is usually denoted by an --INSERT-- notice at the bottom of the screen.  Pressing <ESC> while in Insert mode reverts back to command mode.

Line mode - From command mode, typing a colon ( : ) enters Line mode.  A colon prompt will appear at the bottom of the screen.  Type a command and press <Enter>.   The command will be executed and vi will return to command mode.

The following table lists commands for the most common operations. Commands denoted with a colon are entered in Line mode. All others are entered in Command mode.

Saving Files
:wq Save file and exit
:w Save file but do not exit
:q! Quit without saving
ZZ Save file and exit (same as :wq)
 
Moving Around
Arrow keys Move cursor up, down, left right
G Go to end of file
:n Go to line n
/string Searches for string
n Goes to next match of string
$ Go to end of current line
 
Editing Text
x Delete character 
dd Deletes entire line
D Deletes from cursor to end of line
J Join current line with line below
u Undo last change
yy Cut the current line
p Paste a line


Bash Scripting

Once you become familiar with a text editor such as vi, you can use it to write bash scripts to automate routine tasks. A basic bashscript is just a text file containing a series of commands that would otherwise be typed at the command prompt. As long as the script starts with the following line, the text file can be executed like any other program:

#!/usr/local/bin/bash

Bash scripting also supports more complicated programming structures such as conditional statements and for/while/until looping. Like any good programming language, bash scripts can contain variables. These are defined in the same manner as environment variables and have the same scope properties.

It is usually a good idea to either redefine the PATH environment variable or use complete pathnames to all commands in your script. That way the script will be able to find all the programs it calls no matter which user executes it. It is also a good idea to avoid using interactive commands since this will cause a program to halt or fail. For every menu driven command, such as vi, there are other command line programs, like sed and awk, that can perform the same functions.

 

 

Current related exam topics for the Linux+ exam:

DOMAIN 2.0 Management

2.3 Create files and directories and modify files using CLI commands
2.5 Create linked files using CLI commands
2.19 Create, modify, and use basic shell scripts
2.15 Perform text manipulation (for example: sed, awk, vi)


DOMAIN 3.0 Configuration

3.12 Set up environment variables (for example: $PATH, $DISPLAY, $TERM, $PROMPT, $PS1)




Date: November 28, 2005
TechExams.Net
Author: Drew Miller
Comptia A+ Network+ I-net+ Linux+ MCP