Unix Ecology - Studying the Environment

It seems that users of Unix have divided into two camps. There are those people who absolutely love Unix because you can completely reshape the look and feel of Unix to fit your own taste. And there are those that hate it because you can completely reshape the look and feel of Unix into something completely unfamiliar.

What is this all about? Well, while there is one underlying operating system call Unix, you interact with it through something call a shell and the shell controls your environment; the look and feel of Unix. When you log in you begin with the set, default shell. You control how the shell does its work.

The most commonly available shells are the Bourne shell (sh) and the C-shell (csh). Other shells are close relations to these, such as the Korn shell (ksh), the Bourne-again shell (bash) and the Trivial C-shell (tcsh). New users on Bama are given the "remote" shell. This is a special shell that allows e-email via a POP or IMAP client, like netscape, and dial-up networking. If you wish to do work directly on the unix machine or use ftp you will have to change your shell.

You can select the shell you wish to use by going to the Unix Helpers page and selecting "change your unix shell."

Why are they called shells? Because you can invoke them one on top of the other in layers, like the shells of an onion, each time getting a new environment in which to work. You do this by typing the shell's name (i.e. ksh). You can switch to any shell you please as many times as you please. To leave a shell and go back to the previous layer, simply type 'exit'. When you exit, any changes you made to the environment at that level disappear. There are even some commands you issue which invoke new shells to do their work, then exit on their own when finished. While you probably won't be adding layers of shells on your own, this second way of evoking shells (by running commands) is pretty important to consider.

Since most users on Bama will have the Korn shell we'll concentrate on how to customize it.

When you first login the system looks for a file in your home directory called '.profile'. That really is a period at the beginning of the file name. This is the file we will edit first. So using your favorite editor, bring up the file. If you don't have a favorite you can try 'pico'. Type

pico .profile

You'll see a lots of lines beginning with the pound sign (#) and a couple of lines referring to your mail. The lines that begin with '#' are all comments. This file actually doesn't do much. A great deal of setup is handled by a system-wide profile file. Let's add two special features; let it tell you the date and time when you login and change the prompt from $ to your name. To get the date we will execute the Unix command "date". The shell recognizes a variable for the prompt which we can set, called "PS1". In the ksh it will take two steps to set PS1. So put these three lines at the end of .profile and save it. Be careful about capital and lowercase letters:

date
PS1='your-user-id: '
export PS1

Now we want these changes to take effect. You can either logout and login again, or you can type

. ./.profile (that's dot space dot slash dot profile)

Why did we have to type such a funny line to get the changes to take effect? If we had just tried to run the .profile it would have spawned a new shell, executed date, made the changes for that shell, then exited the shell when the commands had finished running, thus forgetting all the changes. By running .profile as we did, we caused it make the changes in the current shell.

Let's try another interesting prompt. How about one that tells you what directory you are in.

Replace the PS1 line you put in earlier with this:

PS1='($PWD): '

PWD is a variable that the shell knows about and it holds the current working directory name.

The dollar sign ($) tells the shell to substitute for PWD with its value. Rerun .profile as you did before and see if you like the new prompt. Finally, let's combine the two types of prompts, and this time, let the computer figure out who you are instead of putting in your user id directly.

PS1={`whoami`}'$PWD: '

There are quite a number of shell variables that you can set or use. One last important variable you should use is LPDEST. It tells the computer where to send your printed output. By default it is set to a machine over in Gordon Palmer Hall. If you are using a machine that is attached or networked to any printer and you have connected to Bama with a telnet such as TeratermPro you should do this

LPDEST=desktop
export LPDEST

Note that TeratermPro is available free at the Software Download Library: http://ncs.ua.edu/software. The generic telnet that comes with Windows is NOT capable of handling this. You can always do the exporting on one line like this:

export PS1 LPDEST (list here any variables you have set)

Are you ready for a little more? Let's start customizing commands so that they behave the way we want. Most Unix commands have options (also called switches) that you can specify when you run the commands. One that many people like to use is the '-i' switch on the delete command (i.e. rm -i filename). This is the "interactive" switch. Using this option, the command will ask you before it deletes to keep you from deleting files you didn't mean to delete. This happens most often when you delete files using a wildcard (such as "*") in the file name. So, how do we make the delete command do this automatically without requiring extra typing from the user. With the alias command, of course.

First you must add one more line to your .profile. It should look like this:

export ENV=~/.kshrc (that's tilde slash dot kshrc)

The "~" refers to your home directory. In this example I've set the variable ENV and exported it all in one step. It is in the file .kshrc that we will put our aliases.

Use your editor to open a file called .kshrc in your home directory. Put in the following line and save the file:

alias rm='rm -i'

To get this alias to take effect you should type

. ./.kshrc

Now whenever you delete a file you will be asked. There are many useful aliases. Here are some of my favorites.

alias del='rm -i'
alias dir=' ls '
alias ls ='ls -F'
alias bye='exit'
alias logout='exit'
alias lo='exit'

Aliases can be very useful. Suppose you always mistype "mroe" when you mean the "more" command. Just do this:

alias mroe='more'

There'll be no more complaints from Unix after that.

Here's one last goody to put in the .kshrc file.

set -o ignoreeof

Now when you type ctrl-d, which is useful for ending some commands (especially in mail), you won't log out by accident if you issue it in the wrong place.

Well, this is just a small example of how one might rearrange their shell. If you decide to use the csh instead you'll have to do some things differently than we did here. The basic idea is the same, however. Now it's time to go out and get that Unix book and look into more customization.

 

© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.