CLI Magic: Environmental issues

44

Author: Joe Barr

One of the things the CLI provides that you just can’t get with the GUI is the ability to pop the hood open and take a look underneath — maybe even tweak a thing or two, if need be. This week — if you can get up out of your GUI strato-lounger — we’re going to take a look at the Bash shell environment, not only to see what’s there, but also to learn how to change it. Come on, pilgrims, we’re taking charge of our environment!The shell in which your CLI resides has an environment of its own. It includes a lot of information used by the shell itself and by applications you run in it. We’re talking about variables containing program names, arguments, and paths. To get a better idea of what sort of information the environment contains, type env at the command prompt and press Enter.

Here’s what the first few lines of mine looks like:

LESSKEY=/etc/lesskey.bin
NNTPSERVER=news
INFODIR=/usr/local/info:/usr/share/info:/usr/info
MANPATH=/usr/local/man:/usr/share/man:/usr/X11R6/man:/opt/gnome/share/man
SSH_AGENT_PID=6004
HOSTNAME=linux
DM_CONTROL=/var/run/xdmctl
GNOME2_PATH=/usr/local:/opt/gnome:/usr
XKEYSYMDB=/usr/X11R6/lib/X11/XKeysymDB
DESKTOP_STARTUP_ID=
HOST=linux

Browsing through the output of the naked env command, you can spot all sorts of important items: various paths are defined, the locations of man pages provided, the system host name is given, terminal program is specified, the name of the shell to be run, and much more.

Remember PS1? The CLI prompt definition we mod’ed and colorized recently? It’s there. So is your USER name, and your EDITOR, and the HISTSIZE, which controls how many lines of command history will be available to you to scroll back through. My environment contains 79 different variables.

The env command also allows you to define, set the value for, or remove variables from the environment, prior to execution of a command. But it’s only a temporary addition, change, or removal. Let’s play with that a bit. We’ll create a new variable called FUNNYNAME and set it to mister.hawg:

warthawg@linux:~> env FUNNYNAME=mister.hawg;env | grep FUNNY
FUNNYNAME=mister.hawg

If I immediately repeat the naked env with the pipe to grep, FUNNYNAME has disappeared. I refer you to the man for additional info on the env command.

You can also get a list of shell variables using the set command. A bare set entered at the command prompt will produce a list of all existing shell variables sorted by variable name. This can make finding a particular variable a much easier chore. Otherwise, it looks just the same as the output from env. The first few lines on my system look like this:

ACLOCAL_FLAGS='-I /opt/gnome/share/aclocal'
BASH=/usr/local/bin/bash
BASH_VERSINFO=([0]="3" [1]="00" [2]="0" [3]="1" [4]="release" [5]="i586-suse-linux")
BASH_VERSION='3.00.0(1)-release'
COLORTERM=gnome-terminal
COLUMNS=105
CPU=i686
DESKTOP_LAUNCH=gnome-open
DESKTOP_SESSION=gnome

So what if you want to make the change “stick” for longer than a single moment? Easy. Use the export command. Let’s change the value of FUNNYNAME to something else, like this:

export FUNNYNAME=w.hawg

Now when I feed grep the output of env looking for something FUNNY, I get:

FUNNYNAME=w.hawg

Of course, the real reason for having these variables in the first place is to make the information available to processes running on your system. A script or command can produce the value of a variable simply by putting a $ character in front of its name. Let’s replace the grep above with something a little more elegant. How about this?

echo $FUNNYNAME
w.hawg

The change in the environment of the export command entered at the CLI above will remain in effect until it is changed again or the session ends, whichever comes first. The environment variables are loaded when the shell is opened, so if you want the change to be permanent you need to take another approach.

I’m using SUSE 9.2, so I can place any environment mods I want to make in the .bashrc file in my home directory. That’s because SUSE sources your $HOME/.bashrc in /etc/profile. Your milage may vary. The distribution you’re running might not source the .bashrc file, or it might favor your .profile file (also on the home directory) instead. Naturally, the files in a home directory apply only to that user. If you want to make an environment change for all users on the system — like admins often do — you would make them in /etc/profile instead of in either file in the home directory.

Wherever you decide to place the commands, you can do it as shown above with the value being provided as an argument to the export command, or you can split it up, assigning the value on one line and exporting it on a following line, like this:

FUNNYNAME=mister.hawg
export FUNNYNAME

One last item — the unset command. Using it, you can make the variable simply disappear from the environment:

unset FUNNYNAME

OK, there you have it. Refer to the man pages for the env, set, unset, and export commands to go deeper into the subject. And be sure to bring up your adventures in the Bash shell environment the next time you’re in the company of Windows users of the opposite gender. They’re impressed by that kind of talk.