Table of Contents
None
Review the main configuration files associated with the bash shell
Write and execute shell scripts
Syntax for logical evaluations, flow controls and loops
Miscellaneous features (Not part of the LPI 102 objectives)
When you type a command at the prompt the bash
shell will use the PATH variable to find which
executable on the system you want to run.
You can check the value of PATHpath using the
echo command:
$ echo $PATH /usr/bin:/bin:/usr/sbin:/usr/X11R6/bin:/sbin/
In fact many variables are needed by the shell to accommodate for each
user's environment. For example PWD, HOME,
TERM and DISPLAY are such variables.
To initialise and declare a variable the syntax is as follows:
VARIABLE=VALUE
Remember not to put any spaces around the
= sign.
Once a variable is declared and initialised it can be referenced by using
the dollar symbol in front as here:
echo $VARIABLE
This declares a local variable (only available for the current process) that can be listed with set. It is possible to get an exported variable (available to all child processes spawned after the variable has been defined) using export. Exported variables are listed with the env command.
When a shell session is started a number of configuration files are read and most of the variables are set.
One can distinguish configuration files which are read at login time and configuration files which are read for each new bash session.
The first file to be read at login is /etc/profile,
after that the shell will search for the files
~/.bash_profile, ~/.bash_login and
~/.profile and execute the commands from the first
available one.
For every new shell (for example if an xterm emulator
is started) these profiles are not read again.
The profiles are used to define exported variable
(eg PATH) that will be available for every subsequent
program.
The runtime control files ~/.bashrc and
/etc/bashrc are sourced every time
a shell is started.
The runtime control files will store aliases and functions.
Notice that non-interactive shells read neither of these files.
Instead a BASH_ENV variable pointing to the file to be
sourced is declared in the script.
function-name ()
{
command1;
command2;
}
You can test which files are being read by adding an
echo Profile line in /etc/profile,
the type:
bash # no profile is read, nothing is printed bash -login # forces bash to read al files, prints Profile
The following commands also control the way bash starts and can be used for testing:
bash -norc bash -noprofile
Notice that any new bash session will inherit
the parent's global variables defined in /etc/profile
and ~/.bash_profile.
The GNU library readline is used by program that expect user input. It also offers extensive vi and emacs style editing functionality.
Example: the readline default editor
setting for bash is emacs.
One can for example use
Ctrl+E
to go to the end of a line. What happens when we next start,
as below, a shell without editing support?
bash --noediting
The files /etc/inputrc or
~/.inputrc are used to control the
readline library.
One can for example link a keyboard combination to an action.
Example options for inputrc:
set editing-mode vi # change the initial editor style Control-o: "> output" # Ctrl+o outputs the string > output TAB: complete # command and file auto-completion set bell-style none # errors warnings are not audible
Finally, when a user logs out, the shell will read
commands from ~/.bash_logout if it exists.
This file usually
contains the clear command which clears the screen
once the shell exits.