Thursday, July 24, 2014

Customizing the UNIX User Environment - Part 1 - Working with UNIX environment - Part 3

Customizing the UNIX User Environment


Working with UNIX environment


Global and local variables



You use variables to customize how a shell looks, behaves, and responds to user input.

There are two kinds of variables: global or environment variables and local or shell variables


The root user sets global variables and stores them in the /etc/profiles file. Global variables affect all users' shells, providing standard parameters for any shell environment.

Programs running in the system have access to global variables, and use them to make behavior decisions.


Users can set theor own local variables, storing them in .profile files in their home directories.

Local variables affect a particular user's shell session only.


Some common global variables include

-EDITOR  - specifies default editor to use for general text edits. Default value is the vi text editor

-PATH  - specified one or more paths that the system will search for executable files when you enter a command at the shell prompt. The default setting is /usr/bin.

-PRINTER - specifies the default pronter. By default this is not set

-PS1 - specifies the prompt displayed in the shell. Default is $

-SHELL - specifies the path to the default shell program. on most systens this would be /bin/sh.

-TERM - specifies the default terminal used to access the system. The default value is vt100.


Some common local variables include

-HOME - specifies the path to the user's home directory

-LOGNAME - specifies the name that the user uses to log in with

-MAIL - specifies the path to the user's mailbox


When you set a global variable in a local context, the global variable gets overrided. Local variables are for customizing an individual user's experience of the system, so they override global variable values - for that user only.


You can view all of the currently set variables in your shell using the env command. Please type env in the console and hit enter to see the currently set variables


You can view the value of an individual variable using the echo command.

bash-2.05a$ echo $PATH


You need to prefix the variable name with a $ symbol in order to reference its contents rather than just typing it's name


In Bourne shell derivatives, you can set individual variables from the shell prompt by simply declaring them using the form variable=value.

The code shown here sets the value of the PS1 variable - the text that the shell displays as a prompt - to the current working directory, followed by a $.


The PS1 variable supports several switches to make it easier to configure, including

-\h - systems hostname

-\s - reference the name of the current shell

-\t - reference the current time in 24 hour format (HH:MM:SS)

-\u - reference your current username

-\w - reference the name of the current working directory


Once you've set the value of a variable you can force any subshells you launch to use the same value, using the export command, as shown here.


bash-2.05a$ PS1='\w$'

/home/ak$ export PS1



Setting variables in the C shell is similar to Bourne variants, but requires the use of the setenv command.

The code shown here sets the value of the ld_library_path variable to /usr/lib.


easynomad2$ setenv ld_library_path variable to /usr/lib



The C shell uses a different set of variables from the Bourne derivative shells. Because some of the variable names are the same, all C shell variables are expressed in lowercase.


No comments:

Post a Comment