![]() |
UNIX,BOLUM 3 | home
![]() ![]() ![]() UNIX: Customizing Your Environment
![]() ![]() ![]() ![]() ![]() ![]() ![]() Unix is popular with many users, especially programmers, because of the ease with which you can customize your computing environment. Within Unix you can create commands, configure various settings to make the environment suit your tastes and needs, and create helpful shortcuts for accomplishing complex tasks. These feats are primarily accomplished by special files which are automatically executed each time you login. The names of the files may vary depending on the type of shell you are using. For the C shell, they are the .login and .cshrc files. For the Korn shell, the file of primary importance is the .profile file.
To manipulate these files with any proficiency, you need to be acquainted with at least one Unix editor. The simplest of these is probably pico; vi and emacs editors offer greater power. Any editor will suffice for our purposes. In addition, it is assumed that you have a basic working knowledge of Unix. You should be familiar with hierarchical directory structures and have mastered basic Unix commands (ls, cd, chmod, cat, more, etc.). You should also be familiar with some more advanced concepts, such as pipes and redirection. You can find discussions, respectively, in the ~reeves/prog/geoe314/unix01.html"Unix: Getting Started and ~reeves/prog/geoe314/unix02.html"Unix: Data Tools.
The kind of shell you use is the single most important setting determining your environment. There are a variety of shells available. Each of them has different capabilities and drawbacks. The Solaris workstations support the csh and the tcsh, which is a modified version of the C shell. To check if you are running one of these two shells, do the following:
The simplest way of telling which shell you are using is by looking at your prompt: if it is a %, then you are probably using csh ot tcsh. If it is a $, for example, then you are probably using either the Bourne shell (sh) or Korn shell (ksh).
If you cannot tell from the prompt, then at the prompt, type finger yourid; where yourid is your actual user id. This should produce an output like the following (it may look somewhat different depending on your configuration):
Login name: reeves In real life: Malcolm Reeves
Directory: /home/pangea/reeves Shell: /usr/local/bin/tcsh
On since Dec 28 13:56:37 on pts/3 from :0.0
The field which concerns us is the Shell field: this tells us what shell runs when we login to the system. In my case, when I use pangea I use a T shell indicated by tcsh. Other possible shell indicators are csh for C shell, ksh for Korn shell, and sh for Bourne shell
Or, type at the prompt: echo $shell. This will print out the shell currently being run.
Where are the control panels?
Unlike using Windows (barf) or Mac operating systems, there are no convenient buttons or slider switches for changing your settings. Instead, you have to type the proper command for each setting. Although this may make Unix seem arcane and complex, don't be put off by words such as "variable" and "script." Those same qualities, which in Macintosh and Windows make it so easy also limit your options. In comparison, as a Unix user, you have access to virtually every important setting for your environment. The more you use it the less difficult it becomes.
Where are these settings stored?
Most of your settings are stored in a list of variables. These variables generally come in two varieties: shell variables and environment variables. There is one major difference: environment variables are common to every shell you initialize during your session and have global scope; shell variables are specific to the shell in which they are defined. For example, the following is a list of the some of the environment variable settings in my pangea environment:
TERM=dtterm
HOME=/home/pangea/reeves
SHELL=/usr/local/bin/tcsh
USER=reeves
PATH=.:/home/pangea/reeves/bin:/usr/ucb:/bin:/usr/bin:/usr/local/bin
:/usr/openwin/bin:/usr/dt/bin
LOGNAME=reeves
SHLVL=1
PWD=/home/pangea/reeves
HOST=pangea
HOSTTYPE=sun
PAGER=more
EDITOR=vi
VIRTUAL=vi
DISPLAY=thorin:0.0
Each of these values (and many more) are defined for every shell and process that I run during my login session. The type of value depends on the specific variable: some have numeric values, some have string values. To determine if a variable is set or defined within a particular shell, type: echo $variable. If the variable comes back undefined in your new shell, then it is a shell based variable.
How do I initialize a variable?
Actually, it isn't that hard to assign a value to a variable. First, determine if the variable is a shell or environment variable. For environment variables, type:
% setenv VARIABLE value
For shell variables, type:
% set variable=value
These commands are specific to C and T shells, the method is different for the Bourne shell family (sh, bash, ksh). For shell variables, type:
$ variable=value
For environment variables, type:
$ VARIABLE=value
$ export VARIABLE
I'm getting tired of setting all these variables - isn't there a quicker way?
Yes, actually there is. If you type ls -a at the prompt, you'll notice that there is a pair of files in your account, one named .login and the other named .cshrc. Each of these is an example of a shell script (we'll discuss these in more detail later) containing a number of commands executed automatically upon login; among these commands are several important defining variables. The difference between the .login and .cshrc, is that the commands within the .login file are only executed when you first login, but not when you start up subsequent shells in the same session. So the .login is a good place to store environmental variables, since these will need to be reinitialized every time you start a new shell. On the other hand, for every new C shell you start all the commands in your .cshrc are executed. This makes the .cshrc a better place to store definitions for shell variables; you will not need to redefine them for every shell you start. Some environment variables, such as LOGNAME and HOST, are defined automatically, so you need not worry about these.
On the departmental system, common .login and .cshrc files are supplied. These are copied from a common source to ensure a uniform environment for all users. The files are actually stored in /usr/local/etc/skel/local.login and /usr/local/etc/skel/local.cshrc. You may add lines to these files but NEVER delete the first line!
Path Variables
One environmental variable of great importance is the path. The path variable contains a set of directories separated by colons. When you enter a command at a Unix prompt, these directories are searched in the order specified by the path variable. The search process stops at the first occurrence of the file whose name matches the one you entered. Let's say that you have retrieved a file from the Internet, such as gunzip (which is an executable file that uncompresses any file with a .gz extension). If we look at the path environment variable, we see a set of locations where we might find the gunzip file:
PATH=.:/home/pangea/reeves/bin:/usr/local/bin:/usr/bin:/usr/sbin:
/opt/SUNWspro/bin:/opt/gnu/bin:/usr/dt/bin:/usr/ccs/bin:/usr/ccs/lib:
/usr/X11/bin:/usr/openwin/bin/xview:/usr/openwin/bin
The first entry in the path variable is "." This tells the C-shell to begin every search in the current directory. Next is ${HOME}/bin, which translates to a personal bin directory located off of the user's home directory. You might place the gunzip program in this directory, or you could create a directory with the mkdir command (For example: mkdir gnu). If you place the gunzip program into your newly created gnu directory, you would have to call it out by its full pathname, ${HOME}/gnu/gunzip. Or, we could update the path variable to include this newly formed directory.
Using an Unix editor, edit the .cshrc file. Add the line
${PATH}=${PATH}:${HOME}/gnu
When you have finished entering text, return to the command mode and exit the editor saving the changes you have made.
The percentage sign (or dollar sign, in some cases) is your prompt. Its presence simply signals that the shell is ready and waiting for your next command. You are not limited to a percentage sign, 'though. Some people like to customize their prompts to include various pieces of information.
Can I create one of those cool custom prompts?
It is quite easy to modify your prompt so that it displays certain information or a custom message. The prompt information is stored in a variable, specifically the prompt (no surprise there) variable. This variable is normally initialized in the .cshrc. To change the value assigned to it, you need to edit .cshrc:
set prompt = "...%"
You can display a variety of things in the prompt. In addition to the date, time, and current working directory, you can put the hostname and your userid. Depending on the system or version of Unix you are working with, there are even some formatting commands for reverse or underline text. To display these types of information and formats, you need to include between the quotes in your prompt string one or more control characters. These characters consist of an alphabetic character preceded by a percentage sign. For example:
set prompt = "%w %t \n%d%"
From right to left, the %w specifies the date in Mon DD format, the %t gives the time in twelve hour format, the \n is the "newline" character, telling the shell to begin a new line, and the %d gives the current working directory with the complete path. Note: these characters are not universal to all versions of the C shell. To get the same prompt on some systems you would need to type:
set prompt = "%w %D %t \n%/%"
Finding out just what the proper control sequences are can be difficult and confusing. For most shells on most systems, you can pull up the shell man page by typing at the prompt:
% man csh
You can, of course, replace "csh" with whatever shell you are using at the time. Unfortunately, on most systems the information about the prompt setting comes fairly late in the man page.
That's nice, but am I stuck with the same old look?
No, you can set not only the content of the prompt, but also the format. If you insert one of the following format specifiers into your prompt description, everything after it will be in the specified format. To end the format, use the corresponding character (these are usually lowercase).
Format Begin End
boldface %B %b
standout %S %s
underline %U %u
For example, typing this initialization:
set prompt = "%U%t%u %B%n%b%"
should produce this prompt:
11:43 AM johndoe%
Source Command
All changes that you make in the .cshrc file will show up on your next login, and all successive logins. To use the changes you have made in your current session, use the source command, (type: source ~/.cshrc or source ${HOME}/.cshrc). When you source a file, you are telling Unix to use the information in this file as the basis for any environmental definitions. Once you have sourced the updated .cshrc file, the C-shell will find and implement all your new variable definitions.
While the range of commands available on a Unix based system is significantly broader than those on as system such as DOS, you may still wish for a tool you simply don't have. For instance, you want a command to apply only to directories; or maybe you want to cycle through a series of files, adding a line to each; or you simply want to be able to page the output of a particularly lengthy listing. In these cases, and others, where the specific command or tool does not exist, you can create your own. Unix is particularly flexible in the way it allows you to create custom utilities to perform repetitive, boring, or complicated tasks.
Basic Tool Creation - Aliases
Some Unix commands are reasonably short and somewhat intuitive, but this is often not the case. Perhaps you have had to enter a command such as the following before:
% find /usr/local -name xv -print
This command searches in the directory structure under /usr/local for a file named xv, and then, if it finds the file, the path is printed to the screen.
Or, perhaps you have entered a command like the following:
% ls -alR | more
This command gives a complete listing of all the files within a directory and any subdirectories, and then pages the output. Both of the preceding commands are unwieldy to type. The second one especially is the sort of command you might use quite often. In cases like these, you do not need to type the entire command every time; rather, you can create an alias for the command. An alias is a pseudonym you assign to an individual command or a series of commands. You can then execute the command(s) by simply typing the alias and pressing <enter>. The computer will execute all the commands which we assigned to the alias. The command to create an alias is as follows:
% alias aliasname "command(s)"
Here the second argument should be the command(s) you wish to perform. Remember: separate multiple commands by either semicolons or pipes. The aliasname should simply be a short, easy-to-remember nickname which you will type in place of the command(s). For example:
% alias la "ls -al | more"
This allows me to type at the prompt la resulting in a long listing of all the files in my immediate working directory with the output paged. There is no need for me to remember all the commands or worry about including the proper switches. The computer will store these as part of the alias' definition. In this way I can automate the complicated tasks I find myself performing fairly often, simply by creating a different alias for each one. It's important to assign well-thought-out names to aliases. An alias does no good if you cannot remember its name!
Note the alias you have just created is only defined for that login session.. To make an alias automatic for each login session, you will need to add the alias command as a line in your .cshrc file. Using the editor of your choice, open the .cshrc file, add the line
alias aliasname "command(s)"
, exit and save the file.
If I forget my aliases...?
If you find that you have created aliases and you can no longer remember their names, or perhaps you simply created so many that remembering all of them has become impossible, then you should do two things. First, you should probably do away with some of the less useful of your aliases. Aliases are really best used for particularly common or complex tasks, ones which you find yourself performing fairly often. Second, if you want to get a list of the aliases created or assigned, type:
% alias
with no arguments. This will return an output much like the following (but probably not as long):
back set back=$old; set old=$cwd; cd $back; unset back; dirs
big uncompress
bye logout
cd set old=$cwd; chdir !*
cls clear
job uptime; echo " USER PID %CPU %MEM SZ RSS TT STAT TIME
k9 kill -9
la ls -al|more
Some of the most common Unix commands are actually aliases to other, more difficult-to-remember commands: these include the cd, job, and back commands.
How do I remove an alias?
To remove an alias, type:
% unalias aliasname
You may find in your Unix experience that creating an alias is not sufficient for you needs. A shell script is basically a type of program, but one which does not necessarily involve knowledge of a computer language beyond the Unix commands you normally use. Other help files deal with this topic in detail (See: ~reeves/prog/geoe314/unix06.html"UNIX: Scripting).
Remember, the .cshrc and .login files are really nothing more than shell scripts themselves; every line is treated as a command to be executed.
OK, you've just finished typing in that monster four command pipeline. You check the output, and you realize...you need to do the exact same command again!
Oh, come on. Do I really have to type the whole command again?
Well, actually, no. Often, using Unix seems like one large typing test, where any typo spells disaster. But you can avoid the tedium of typing the same commands over and over again because Unix remembers the commands you've typed in. Unix stores these commands in two variables, the history and savehist variables. The first of these, history, stores a set number of commands during the current session. At any time you can access this group of commands using a variety of shortcuts. The second variable, savehist, stores a set number of commands from your previous login session.
How do I know how many commands Unix will remember?
In your .cshrc file there will usually be a command like this: set history=40. This tells Unix to remember your last forty commands. Each time you execute a command at the prompt, it becomes command number forty, and the oldest command in the history is deleted. If you need to access more than your last forty commands, you can simply add the line in your .cshrc to preserve more than forty, something like set history=75.
Can I see what my commands were?
Absolutely. You can see all the commands stored by simply typing at the prompt
% history
This will display a list of every command in your history, with the history command last in the list. Beside each command will be a number, marking its position in the list. These are ordered first-to-last. A convenient alias ``h'' is provided in our ``default'' .cshrc to list these commands a page at a time. To re-enter one of the commands in your history, use the following keystrokes:
Keystroke Action
!! repeat last command
!# do the numbered command (e.g., !5 does your fifth command)
!-# do the numbered command relative to you (e.g., !-5 executes
the command done five back)
!x find and execute a command that began with x (e.g., !l
might find a ls command you did)
!?x find the last event that contains x anywhere in the command
Using history processes in your shell
The T-shell itself has a history mechanism. You can access your history commands, (starting backwards from your most recent entry), by using the up arrow key. These commands are kept in an editable buffer; use the arrow keys to relocate the cursor along the command line. You can then edit the command until you have it configured the way you want it. Press the <enter> to implement the command .
A Last Shell Command-Editing Trick
You can change characters in the last command you entered through use of the ^. The example below illustrates this property:
% ls prgram
prgram: No such file or directory
% ^prg^prog
-rwx--x--x 1 joeuser nobody 85772 Dec 27 17:15 program*
As you can see, the ^ corrects the mis-spelled entry in the first command.
|
![]() |