The linux shells contains a comprehensive list of built in
shell environment variables. A shell environment variable can be set as
a local variable or a global variable. A local variable is only known
to the shell and not to any external program. Shell variables are
required to be set as global so that programs external to the shell
like matlab, tecplot and etc, can interface with the operating system.
Most compute intensive packages need to have access to the tmp
directory so a shell variable is set up where by all programs can
access the tmp directory. Common shell variables follow a certain
standard and they are in capitol letters.
A user can also define a shell variable. The common commands used in
Shell operations are
- echo $name
- env
- typeset -f or typeset -F
- export
Common shell functions are
- PATH
- alias
Common control characters are
- ""
- ''
- $
- #
The
alias command allows the user to customize long commands into one
single command. The alias command is commonly used when one uses Unix
commands with flags. Remembering the flags can be tedious so an alias
is set up as shown below.
Note: In the bash shell the syntax is strict and must be followed
explicitly. The command must start at column1 of the editor and there
should be no spaces on either side of the "equal to" (=) sign.
The commands that are aliased appear on the right side of the = sign
and
are enclosed in single or double quotes.
There is a slight difference between the single and double quotes: the
single quote will nullify the special meaning of certain characters
like $, !, #. While the double quote will retain them.
| command |
alias in .cshrc |
alias in .bashrc
|
what
does it do
|
| ls -F |
alias ls 'ls -F' |
alias ls='ls -F
--color=auto'
|
adds
color to the ls
|
| rm -i |
alias rm 'rm -i' |
alias rm='rm -i'
|
removes
with ?
|
cp -r -i
|
alias cp 'cp -r' |
alias cp= 'cp -i
-r'
|
copies
directories also
|
| rm -rf |
alias rm_dir 'rm -rf' |
alias rm_dir='rm
-rf'
|
remove
directories and contents with ?
|
WHERE TO PUT THE ALIAS COMMANDS
The
alias commands are placed in the
.bashrc file found in /home/joeusr.
Place the commands after the following segment of lines. Make sure that
anything you add is after this block of code in red
# .bashrc
# User specific aliases and functions ####################################################################################################### # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi ######################################################################################################## #My user defined set of alias commands alias rm="rm -i" alias cp="cp -r -i" alias ls="ls -F --color=auto" alias mv="mv -i" alias compile="ifc -c -w -g" alias link="ifc -g" alias clink="ifc -w -g" #My prompt export PS1="[\h\w]:"
#Running executables without ./ export PATH=$PATH:.
|
BASH PROMPT AND CUSTOMIZATION
The bash prompt on the left hand corner of the shell can be customized
to the user's preference.
Save all these files in your home directory i.e., /home/joerusr/
SETTING PROMPT FOR bash
The bash prompt is governed by the PS1 environment variable. The PS1
variable can be modified by the user and placed in the .bashrc
file.
export
PS1="[\w \h ]\\>>"
|
Here is an example of a customized multiline
bash prompt
OPTIONS FOR THE BASH PROMPT
| Character |
what it does |
| \d |
Date |
| \h |
Hostname |
| \n |
Carriage
Return |
| \s |
The
shell now working |
| \t |
The
time shown as 24 hours |
| \T |
The
time shown as 12 hours |
| \@ |
The
time shown as 12 hours |
| \u |
Username |
| \v |
which
version of bash |
| \V |
version
of bash in detail |
| \w |
Current
directory from ~/ |
| \W |
Just
the name of the current directory |
| \! |
History
number of the command now working |
| \# |
Command
number of the command now working |
| \$ |
Showing
"#" if the user is root, "$" if others |
| \\ |
"\" |
Shell
variables are commonly used when programs need to access default
settings, directories and system files. They can be user defined and
are standardized so that they can be invoked across various external
programs. Suppose there is a program that requires an external editor,
and somewhere in the programs start up script the variable $EDITOR is
used. The $ preceding the variable name tells the shell that it is a
shell variable. In order for the program to access $EDITOR the variable
EDITOR needs to be set. What if the system admin forgot to do so and
thats where you come in.
<bash>echo
$EDITOR
|
see if the variable
has been set. If not the field will be null
|
<bash>export
EDITOR=nedit
|
set the editor
variable to nedit
|
<bash>echo
$EDITOR
|
nedit should echo
|
| <bash>$EDITOR |
Use the variable to
invoke the nedit editor
|
<bash> echo
$HOSTNAME
|
hostname of the
machine should echo
|
<bash> export
TEC10HOME=/usr/local/tecplot10/bin
|
create an
environment variable for the tecplo10 dir
|
<bash>
$TEC10HOME/tecplot
|
tecplot10 should
start
|
<bash> echo
$USER
|
your username should
echo
|
<bash> echo
$HOME
|
your home dir
|
<bash> echo
$LD_LIBRARY_PATH
|
common environment
variables that all program scripts require to access system libs.
|
The PATH variable is the most important environment variable in the system.
If the PATH variable is broken the shell would be rendered useless or
incapacitated and would not be able to execute any commands. One of the
reasons the "bash shell" was chosen over the "tom shell" was due to the
PATH variable and how the two shells interacted with it. The PATH
variable is nothing but a repository of various directory paths where
applications can be found. When the user executes a command the shell
goes looking for that particular program in all the directories listed
in the PATH variable. As soon as the shell finds what it is looking for
the command is executed. If the directory is not listed in the PATH
variable list then "command not found" will be echoed.
ATTENTION:
Bring up a shell and only use this shell. When done with the exercise
kill the shell
|
|
command
|
Results
|
<bash> echo
$PATH
|
Will list a messy
list of all directories in the PATH list
|
<bash> which ls
|
Should echo the path
where the ls command resides
|
<bash> unset
PATH
|
This will unset the
path
|
<bash> ls
|
the ls command will
not work
|
<bash>ifc
|
the compiler will
not work
|
<bash> CTRL-D
|
kill this shell and
bring up a new one
|
When a new a directory needs to be added to the PATH
the user appends the directory to the already existing $PATH variable.
The user can add directories to the PATH variable by editing the
.bashrc file or the .bash_profile file. If the .bash_profile is used
for updating the PATH variable then the user needs to logout and log
back in or invoke a konsole from the existing konsole with the -ln flag
The .bash_profile is updated only through a login shell or on login.
This is to optimize the startup sequence otherwise if the PATH variable
has a lot of dirs listed then every time a new shell is opened all the
PATH variables will have to accessed and this may take a while if the
machine is slow. For now you can use the .bashrc file since you will
not be adding too many dirs to your PATH.
# .bashrc # once you update this file save and exit the file and then open a new shell or use # <bash> source .bashrc
# User specific aliases and functions ####################################################################################################################### # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi ####################################################################################################################### #My user defined set of alias commands alias rm="rm -i" alias cp="cp -r -i" alias ls="ls -F --color=auto" alias mv="mv -i" alias compile="ifc -c -w -g" alias link="ifc -g" alias clink="ifc -w -g" #My prompt export PS1="[\h\w]:"
#Adding to the PATH variable #Running executables without ./ export PATH=$PATH:. export PATH=$PATH:/usr/local/tecplot10/bin
|
WHEN TO USE " AND '
There is difference between the " " and the ' ' quotes in
the bash environment. Anything enclosed in single quotes will lose its
special meaning e.g. a $PATH statement in single quotes will literally
mean $PATH and not the PATH variable. Well, the only way to understand
this is to try it
command
|
results
|
<bash> echo
'$PATH'
|
It should echo
exactly what the user typed
|
<bash> echo
"$PATH"
|
It should echo the
contents of the PATH VARIABLE
|
<bash> echo
"Bond, James Bond"
|
When echoing
statements with spaces use the double quotes
|
|