Sunday, July 20, 2014

Linux/Unix - Shell variables & environment

Shell environment

  • The shell maintains state data to assist the user (and to pass configuration data to commands)
  • The state data is held in variables
    - normally string valued
    - some variables are pre-defined when the user logs in
    - further variables may be created by the user
  • There are 2 kinds of variables:
    - local variables or shell variables visible only within the shell
    - global or environment variables visible to all commands invoked from the shell

Environment Variables - Pre-defined

An example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. Type
% echo $OSTYPE
More examples of environment variables are
Variable Significance
USER or user OR LOGNAME your login name
HOME or home the path name of your home directory
HOST the name of the computer you are using
ARCH the architecture of the computers processor
DISPLAY the name of the computer screen to display X windows
PRINTER the default printer to send print jobs
PATH or path the directories the shell should search to find a command
PS1 The shell's primary prompt
PS2 The shell's secondary prompt
SHELL The path name of the shell
TERM The type of terminal
IFS Input field seperator
MAILCHECK The frequency of email checked

Note that small cased variables are not environment or global variables. They are shell or local variables.

Finding out the current values of these variables.

ENVIRONMENT variables are set using the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command.
To show all values of these variables, type
% printenv | less

Shell Variables

An example of a shell variable is the history variable. The value of this is how many shell commands to save, allow the user to scroll back through all the commands they have previously entered. Type
% echo $history
More examples of shell variables are
  • cwd (your current working directory)
  • home (the path name of your home directory)
  • path (the directories the shell should search to find a command)
  • prompt (the text string used to prompt for interactive commands shell your login shell)

Finding out the current values of these variables.

SHELL variables are both set and displayed using the set command. They can be unset by using the unset command.
To show all values of these variables, type
% set | less

So what is the difference between PATH and path ?

In general, environment and shell variables that have the same name (apart from the case) are distinct and independent, except for possibly having the same initial values. There are, however, exceptions.
Each time the shell variables home, user and term are changed, the corresponding environment variables HOME, USER and TERM receive the same values. However, altering the environment variables has no effect on the corresponding shell variables.
PATH and path specify directories to search for commands and programs. Both variables always represent the same directory list, and altering either automatically causes the other to be changed.

Shell commands

  • Commands used to declare and manipulate shell variables:
  •      set            - assigns non-numeric string variables 
         unset          - removes a previously "set" variable 
         set            - shows all "set" variables
         setenv         - assigns non-numeric string variables 
         unsetenv       - removes a previously setenv variable
         setenv         - shows all setenv variables
         @              - assigns numeric variables locally
         echo $variable - displays value of variable
  • Examples
  •      set name=fred               - Sets variable name to 
                                       value of fred
         unset name                  - Unsets the variable name
         set path=($path . ~/bin)    - Adds to current setting 
                                       of string array variable 
         setenv DISPLAY farragut:0     - Sets environment variable 
         echo $HOME                  - Displays value of variable 
         set colors=(red green blue) - Assigns three values to 
                                       the string array variable 
         @ count = 1                 - Sets numeric variable 
                                       count to one
         @ count = ($count + 1)      - Adds one to numeric 
                                       variable count
         set counts = (1 22 4 9)     - Assigns 4 values to 
                                       numeric array variable 
         @ counts[2] = 5             - Assigns values to second
                                       element of numeric array
                                       variable counts
         @ echo $counts[3]           - Displays value of third
                                       element of numeric array
                                       variable counts


Using and setting variables

Each time you login to a UNIX host, the system looks in your home directory for initialization of files. Information in these files is used to set up your working environment. The C and TC shells uses two files called .login and .cshrc (note that both file names begin with a dot).
At login the C shell first reads .cshrc followed by .login
.login is to set conditions which will apply to the whole session and to perform actions that are relevant only at login.
.cshrc is used to set conditions and perform actions specific to the shell and to each invocation of it.
The guidelines are to set ENVIRONMENT variables in the .login file and SHELL variables in the .cshrc file.
WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .cshrc or .login file.

Setting shell variables in the .cshrc file

For example, to change the number of shell commands saved in the history list, you need to set the shell variable history. It is set to 100 by default, but you can increase this if you wish.
% set history = 200
Check this has worked by typing
% echo $history
However, this has only set the variable for the lifetime of the current shell. If you open a new xterm window, it will only have the default history value set. To PERMANENTLY set the value of history, you will need to add the set command to the .cshrc file.
First open the .cshrc file in a text editor. An easy, user-friendly editor to use is nedit.
% nedit ~/.cshrc
Add the following line AFTER the list of other commands.
set history = 200
Save the file and force the shell to reread its .cshrc file buy using the shell source command.
% source .cshrc
Check this has worked by typing
% echo $history

Initialization Files

  • System-wide shell initialization files are common on UNIX systems. These files can be "sourced" automatically by the shell and are typically setup by the System Administrator for the local environment.
  • Some examples of system-wide initialization files might be:
  •      /etc/environment
  • Every shell provides a means for the user to customize certain aspects of the shell's behavior. These customizations usually permit you to augment and/or override the system-wide defaults.
  • User customizations are specified in initialization files located in the top level of your home directory.
  • Naming: Depending upon the shell, you must name your initialization file(s) accordingly.
  • Executed during interactive login 
          .login        - csh, tcsh
          .profile      - sh, ksh, bash
          .bash_profile - bash (alternative 1)
          .bash_login   - bash (alternative 2)
    Executed for every new shell 
          .cshrc        - csh, tcsh
          .tcshrc       - tcsh
          .kshrc        - ksh
          .bashrc       - bash
  • The C shell uses two files to set user preferences:
    • .cshrc
      • runs at each login before the .login file
      • runs whenever another shell is created
      • runs when a new window is opened
      • runs when many utilities are invoked
      • typically sets alias information, path variable, filec, prompt, etc.
    • .login
      • runs at invocation of login shell, after the .cshrc file
      • typically sets terminal characteristics and one time shell options and environment variables
  • After changing your .login or .cshrc files, you must "source" them for the changes to take effect:
  •      source .login
         source .cshrc
  • The system administrator may/may not provide you with default .cshrc and .login files when you first obtain your userid. If they are provided, be careful about modifying them - especially removing specifications which are required for your local system.
  • Example .cshrc and .login files are provided below:

Logout files

  • You are able to specify commands which the shell will execute upon logout. These commands are kept in a file located in the top level of your home directory.
  •      .logout>        - csh, tcsh 
         .bash_logout    - bash 

Setting the path

When you type a command, your path (or PATH) variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "command: Command not found", this indicates that either the command doesn't exist at all on the system or it is simply not in your path.
For example, to run units, you either need to directly specify the units path (~/units174/bin/units), or you need to have the directory ~/units174/bin in your path.
You can add it to the end of your existing path (the $path represents this) by issuing the command:
% set path = ($path ~/units174/bin)
Test that this worked by trying to run units in any directory other that where units is actually located.
% cd
% units
To add this path PERMANENTLY, add the following line to your .cshrc AFTER the list of other commands.


Post a Comment