-CONTINUED
In the above, the
commands ls , haha , sl ,
will all perform the same action when typed.
That is: ls -aF --color . This is the
usefulness of aliases. One particular use of aliases
is for safety precautions:
alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'
In the above example,
the commands rm , mv , and
cp will all ask for confirmation before
attempting to overwrite a file. This is a good thing
so you do not have to type the long form
command with options. Another example:
alias mycd='mount -t iso9660 /dev/cdrom /cdrom'
You can now mount
the CDROM with the command mycd , although
a much better way
would be to actually edit /etc/fstab ,
but this example serves the purpose as well. To
find out what your current aliases are, you use the
alias command:
xconsole$ alias
alias cp='cp -i'
alias rm='rm -i'
alias mv='mv -i'
Aliases can also
be set and unset while the shell is running. Setting
an alias on a shell is
done with the alias command. Example:
xconsole$ alias mydir='ls -alF'
You can also unset
aliases with the unalias command:
xconsole$ unalias mydir
The unalias
command has one option: -a . This option
allows you to unalias all
currently set aliases.
FUNCTIONS
Functions normally go to /etc/bashrc
and ~/.bashrc . Functions are pieces of
code
that perform a special action when called upon from
the shell. You will need to know
bash shell programming in order to use functions.
Here is an example of a function that
unpacks all tarballs in the current directory:
unpack()
{
for tarball in *; do
tar xvzf $tarball
done
}
Now if you wanted
to unpack all tarballs in a current directory, all
you would have to
do is to use the command unpack , and
all tarballs will be unpacked. Of course this is
a really simple function, and could have been used
as an alias instead:
alias unpack='tar xvzf *.tgz'
However, the above
function is very simple. You can write much more complex
functions, depending on the job to be accomplished.
You can put entire shell
programs into the /etc/bashrc or the
~/.bashrc files and have them run just
by typing their function name.
EXAMPLES
The following are examples of what the bash
configuration files can look like.
Feel free to create and/or modify your own configuration
files. It is your system,
and you should decide how you want it to run.
/etc/profile
# /etc/profile: This file contains system-wide
defaults used by
# all Bourne (and related) shells.
# Set the values for some environment variables:
export OPENWINHOME=/usr/openwin
export MINICOM="-c on"
export MANPATH=/usr/local/man:/usr/man/preformat:
/usr/man:/usr/X11R6/man:/usr/openwin/man
export HOSTNAME="`cat /etc/HOSTNAME`"
export LESSOPEN="|lesspipe.sh %s"
export LESS="-M"
#export MOZILLA_HOME=/usr/local/netscape
export HISTSIZE=20
export HISTFILESIZE=20
# Set notification when a job ends.
set -o notify
# Set the default system $PATH:
PATH="$PATH:/usr/X11R6/bin:$OPENWINHOME/bin:
/usr/games"
# I had problems using 'eval tset' instead of
'TERM=', but you might want to
# try it anyway. I think with the right /etc/termcap
it would work great.
# eval `tset -sQ "$TERM"`
if [ "$TERM" = "" -o "$TERM" = "unknown" ]; then
TERM=linux
fi
# 'kvt' uses "xterm-color" which isn't recognized by
programs like 'pine'
# or 'pico', so let's change it to the default "xterm":
if [ "$TERM" = "xterm-color" ]; then
TERM=xterm
fi
# Set a default shell prompt:
PS1='[\u@\h \W]\$ '
PS2='> '
ignoreeof=10
export PATH DISPLAY LESS TERM PS1 PS2 ignoreeof
# Default umask. A umask of 022 prevents new files
from being created group
# and world writable.
umask 022
# Set up the LS_COLORS and LS_OPTIONS environment
variables for color ls:
eval `dircolors -b`
# Notify user of incoming mail. This can be overridden
in the user's
# local startup file (~/.bash.login or whatever,
depending on the shell)
if [ -x /usr/bin/biff ]; then
biff y
fi
# Print a fortune cookie for login shells:
echo
fortune /usr/games/lib/fortunes/fortunes /usr/games/lib/fortunes/fortunes2
echo
# Environment variables for the Qt package:
QTDIR=/usr/lib/qt
CPLUS_INCLUDE_PATH=$QTDIR/include:$CPLUS_INCLUDE_PATH
export QTDIR
export CPLUS_INCLUDE_PATH
# KDE additions:
KDEDIR=/opt/kde
PATH=$PATH:$KDEDIR/bin
export KDEDIR PATH
# Make sure that the current directory is always last
in the PATH variable. PATH="$PATH:."
/etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment
stuff goes in /etc/profile
# functions go here
# create a new ~/.plan file at every log in
new_plan()
{
if [ -f `which fortune` ]; then
echo > ~/.plan
`which fortune -s` >> ~/.plan
echo >> ~/.plan
fi
}
# aliases go here
# paranoia
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# user specific environment and startup programs
PATH=$PATH:$HOME/bin:$HOME/garbage
BASH_ENV=$HOME/.bashrc
export PATH BASH_ENV
~/.bashrc
#.bashrc
# User specific aliases and functions
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# create a log of all users who logged in,
# and those currently logged in
see_all()
{
LOG_FILE=/var/log/see_all
echo `date` >> $LOG_FILE
echo "Recent users to log in: " >> $LOG_FILE
last | head -10 >> $LOG_FILE
echo "Users who are currently online: " >> $LOG_FILE
who >> $LOG_FILE
}
# no pine-debug[?]
alias pine='pine -d0'
# special ls
alias ls='ls -aF --color'
~/.bash_logout
# .bash_logout
echo "You logged off at `date`"
echo "See you again `whoami`!"
sleep 2
clear
Let your imagination run wild. The shell is powerful
enough to take in complex commands and functions.
Examing the current configuration files you have on
your system and tailor them to meet your specific
needs.
CONCLUSION
Hopefully this NHF has been able to guide you into
making your own bash configuration files,
and modifying your current ones. You are also encouraged
to read the manual page for bash : man
bash for those of you who do not know. The
best way to learn is to read the manual and of course,
to experiment.
|