For users of /bin/sh or /bin/ksh, functions provide quick, portable alternatives to aliases and shell scripts. Functions also allow you to change environment variables in your current shell quickly and easily.
One of the most common changes you may make is to your TERM variable,
especially if you dial into a modem from
various packages (each supporting different emulations). In this case
you can use a function to check or change TERM. I call mine "T" since there's
no command on my system that uses that name:
T() {
if [ -z "${1}" ]
then
echo "Your TERM type is set to $TERM"
else
echo "Your TERM was $TERM \c"
TERM=${1}
export TERM
echo "and is now $TERM"
fi ;
}
Put this in either your .profile or /etc/profile (so all logins get it). Type "T" alone tells you the term type, while type "T newterm" ("T ansi", for example) will show you what your TERM was and now is.
Caveats to remember: functions do not carry over to sub-shells, so if you shell out of an application the function will not be available. Functions run before any commands in the PATH with the same name, so if there were a /usr/local/bin/T, the function T would run first unless you call "/usr/local/bin/T" directly.
Functions are often used in shell scripts to organize and replicate
certain steps, but they are not limited to scripts. This is just one example
of how functions can be used. The nice thing about functions is that they
are portable between Bourne shell and Korn shell (and their derivatives).
Return to : Unix System Administration
Hints and Tips