Configuring Your Shell with Shell System Variables

 < Day Day Up > 



When you log in to your account, the system generates a shell for you. This shell is referred to as either your login shell or your user shell. When you execute scripts, you are generating subshells of your user shell. You can define variables within your user shell, and you can also define environment variables that can be referenced by any subshells you generate. Linux sets up system shell variables you can use to configure your user shell. Many of these system shell variables are defined by the system when you log in, but you define others yourself. See Table 9-1 for a list of the commonly used ones.

A reserved set of keywords is used for the names of these system variables. You should not use these keywords as the names of any of your own variable names. The system shell variables are all specified in uppercase letters, making them easy to identify. Shell feature variables are in lowercase letters. For example, the keyword HOME is used by the system to define the HOME variable. HOME is a special environment variable that holds the pathname of the user's home directory. On the other hand, the keyword noclobber is used to set the noclobber feature on or off.

System Variables

Many of the system variables automatically defined and assigned initial values by the system when you log in can be changed, if you wish. Some system variables exist whose values should not be changed, however. For example, the HOME variable holds the pathname for your home directory. Commands such as cd reference the pathname in the HOME system variable to locate your home directory. Some of the more common of these system variables are described in this section. Other system variables are defined by the system and given an initial value that you are free to change. To do this, you redefine them and assign a new value. For example, the PATH variable is defined by the system and given an initial value; it contains the pathnames of directories where commands are located. Whenever you execute a command, the shell searches for it in these directories. You can add a new directory to be searched by redefining the PATH variable yourself, so that it will include the new directory's pathname. Still other special variables exist that the system does not define. These are usually optional features, such as the EXINIT variable that enables you to set options for the Vi editor. Each time you log in, you must define and assign a value to such variables. Some of the more common system variables are SHELL, PATH, PS1, PS2, and MAIL. The SHELL variable holds the pathname of the program for the type of shell you log in to. The PATH variable lists the different directories to be searched for a Linux command. The PS1 and PS2 variables hold the prompt symbols. The MAIL variable holds the pathname of your mailbox file. You can modify the values for any of them to customize your shell.

Note 

You can obtain a listing of the currently defined system variables using the env command. The env command operates like the set command, but it lists only system variables.

Using Initialization Files

You can automatically define system variables using special shell scripts called initialization files. An initialization file is a specially named shell script executed whenever you enter a certain shell. You can edit the initialization file and place in it definitions and assignments for system variables. When you enter the shell, the initialization file will execute these definitions and assignments, effectively initializing system variables with your own values. For example, the BASH shell's .bash_profile file is an initialization file executed every time you log in. It contains definitions and assignments of system variables. However, the .bash_profile file is basically only a shell script, which you can edit with any text editor such as the Vi editor; changing, if you wish, the values assigned to system variables.

In the BASH shell, all the system variables are designed to be environment variables. When you define or redefine a system variable, you also need to export it to make it an environment variable. This means any change you make to a system variable must be accompanied by an export command. You will see that at the end of the login initialization file, .bash_profile, there is usually an export command for all the system variables defined in it.

Your Home Directory: HOME

The HOME variable contains the pathname of your home directory. Your home directory is determined by the system administrator when your account is created. The pathname for your home directory is automatically read into your HOME variable when you log in. In the next example, the echo command displays the contents of the HOME variable:

$ echo $HOME /home/chris

The HOME variable is often used when you need to specify the absolute pathname of your home directory. In the next example, the absolute pathname of reports is specified using HOME for the home directory's path:

$ ls $HOME/reports 

Command Locations: PATH

The PATH variable contains a series of directory paths separated by colons. Each time a command is executed, the paths listed in the PATH variable are searched one by one for that command. For example, the cp command resides on the system in the directory /usr/bin. This directory path is one of the directories listed in the PATH variable. Each time you execute the cp command, this path is searched and the cp command located. The system defines and assigns PATH an initial set of pathnames. In Linux, the initial pathnames are /usr/bin and usr/sbin.

The shell can execute any executable file, including programs and scripts you have created. For this reason, the PATH variable can also reference your working directory; so if you want to execute one of your own scripts or programs in your working directory, the shell can locate it. No spaces are allowed between the pathnames in the string. A colon with no pathname specified references your working directory. Usually, a single colon is placed at the end of the pathnames as an empty entry specifying your working directory. For example, the pathname /usr/bin:/usr/sbin: references three directories: /usr/bin, /usr/sbin, and your current working directory.

$ echo $PATH /usr/bin:/usr/sbin:

You can add any new directory path you want to the PATH variable. This can be useful if you have created several of your own Linux commands using shell scripts. You could place these new shell script commands in a directory you created and then add that directory to the PATH list. Then, no matter what directory you are in, you can execute one of your shell scripts. The PATH variable will contain the directory for that script, so that directory will be searched each time you issue a command.

You add a directory to the PATH variable with a variable assignment. You can execute this assignment directly in your shell. In the next example, the user chris adds a new directory, called mybin, to the PATH. Although you could carefully type in the complete pathnames listed in PATH for the assignment, you can also use an evaluation of PATH, $PATH, in their place. In this example, an evaluation of HOME is also used to designate the user's home directory in the new directory's pathname. Notice the empty entry between two colons, which specifies the working directory.

$ PATH=$PATH:$HOME/mybin: $ export PATH $ echo $PATH /usr/bin:/usr/sbin::/home/chris/mybin

If you add a directory to PATH yourself while you are logged in, the directory would be added only for the duration of your login session. When you log back in, the login initialization file, .bash_profile, would again initialize your PATH with its original set of directories. The .bash_profile file is described in detail a bit later in this chapter. To add a new directory to your PATH permanently, you need to edit your .bash_profile file and find the assignment for the PATH variable. Then, you simply insert the directory, preceded by a colon, into the set of pathnames assigned to PATH.

Specifying the BASH Environment: BASH_ENV

The BASH_ENV variable holds the name of the BASH shell initialization file to be executed whenever a BASH shell is generated. For example, when a BASH shell script is executed, the BASH_ENV variable is checked and the name of the script that it holds is executed before the shell script. The BASH_ENV variable usually holds $HOME/.bashrc. This is the .bashrc file in the user's home directory. The .bashrc file is discussed later in this chapter. You could specify a different file if you wish, using that instead of the .bashrc file for BASH shell scripts.

Configuring the Shell Prompt

The PS1 and PS2 variables contain the primary and secondary prompt symbols, respectively. The primary prompt symbol for the BASH shell is a dollar sign, $. You can change the prompt symbol by assigning a new set of characters to the PS1 variable. In the next example, the shell prompt is changed to the -> symbol:

$ PS1= '->' -> export PS1 ->

You can change the prompt to be any set of characters, including a string, as shown in the next example:

$ PS1="Please enter a command: " Please enter a command: export PS1 Please enter a command: ls mydata /reports Please enter a command:

The PS2 variable holds the secondary prompt symbol, which is used for commands that take several lines to complete. The default secondary prompt is >. The added command lines begin with the secondary prompt instead of the primary prompt. You can change the secondary prompt just as easily as the primary prompt, as shown here:

 $ PS2="@" 

Like the TCSH shell, the BASH shell provides you with a predefined set of codes you can use to configure your prompt. With them you can make the time, your username, or your directory pathname a part of your prompt. You can even have your prompt display the history event number of the current command you are about to enter. Each code is preceded by a \ symbol. \w represents the current working directory, \t the time, and \u your username. \! will display the next history event number. In the next example, the user adds the current working directory to the prompt:

$ PS1="\w $" /home/dylan $

The codes must be included within a quoted string. If no quotes exist, the code characters are not evaluated and are themselves used as the prompt. PS1=\w sets the prompt to the characters \w, not the working directory. The next example incorporates both the time and the history event number with a new prompt:

$ PS1="\t \! ->" 

The following table lists the codes for configuring your prompt:

Prompt Codes

Description

\!

Current history number

\$

Use $ as prompt for all users except the root user, which has the # as its prompt

\d

Current date

\s

Shell currently active

\t

Time of day

\u

Username

\w

Current working directory

Specifying Your News Server

Several shell system variables are used to set values used by network applications, such as Web browsers or newsreaders. NNTPSERVER is used to set the value of a remote news server accessible on your network. If you are using an ISP, the ISP usually provides a news server you can access with your newsreader applications. However, you first have to provide your newsreaders with the Internet address of the news server. This is the role of the NNTPSERVER. News servers on the Internet usually use the NNTP protocol. NNTPSERVER should hold the address of such a news server. For many ISPs, the news server address is a domain name that begins with nntp. The following example assigns the news server address nntp.myservice.com to the NNTPSERVER system variables. Newsreader applications automatically obtain the news server address from NNTPSERVER. Usually, this assignment is placed in the shell initialization file, .bash_profile, so that it is automatically set each time a user logs in.

NNTPSERVER=nntp.myservice.com export NNTPSERVER 

Configuring Your Login Shell: .bash_profile

The .bash_profile file is the BASH shell's login initialization file, which can also be named .profile (as in SuSE Linux). It is a script file that is automatically executed whenever a user logs in. The file contains shell commands that define system environment variables used to manage your shell. They may be either redefinitions of system-defined variables or definitions of user-defined variables. For example, when you log in, your user shell needs to know what directories hold Linux commands. It will reference the PATH variable to find the pathnames for these directories. However, first, the PATH variable must be assigned those pathnames. In the .bash_profile file, an assignment operation does just this. Because it is in the .bash_profile file, the assignment is executed automatically when the user logs in.

Exporing Variables

System variables also need to be exported, using the export command, to make them accessible to any subshells you may enter. You can export several variables in one export command by listing them as arguments. Usually, at the end of the .bash_profile file is an export command with a list of all the variables defined in the file. If a variable is missing from this list, you may be unable to access it. Notice the export command at the end of the .profile file in the example described next. You can also combine the assignment and export command into one operation as shown here for NNTPSERVER:

export NNTPSERVER=nntp.myservice.com

Variable Assignments

A copy of the standard .bash_profile file provided for you when your account is created is listed in the next example. Notice how PATH is assigned, as is the value of $HOME. Both PATH and HOME are system variables the system has already defined. PATH holds the pathnames of directories searched for any command you enter, and HOME holds the pathname of your home directory. The assignment PATH=$PATH:$HOME/bin has the effect of redefining PATH to include your bin directory within your home directory. So your bin directory will also be searched for any commands, including ones you create yourself, such as scripts or programs. Notice PATH is then exported, so it can be accessed by any subshells. Should you want to have your home directory searched also, you can use any text editor to modify this line in your .bash_profile file to PATH=$PATH:$HOME/bin:$HOME, adding :$HOME at the end. In fact, you can change this entry to add as many directories as you want searched.

.bash_profile

start example
 # .bash_profile     # Get the aliases and functions if [ -f ~/.bashrc ]; then           . ~/.bashrc fi     # User specific environment and startup programs     PATH=$PATH:$HOME/bin BASH_ENV=$HOME/.bashrc USERNAME=""     export USERNAME BASH_ENV PATH 
end example

Editing Your BASH Profile Script

Your .bash_profile initialization file is a text file that can be edited by a text editor, like any other text file. You can easily add new directories to your PATH by editing .bash_profile and using editing commands to insert a new directory pathname in the list of directory pathnames assigned to the PATH variable. You can even add new variable definitions. If you do so, however, be sure to include the new variable's name in the export command's argument list. For example, if your .bash_profile file does not have any definition of the EXINIT variable, you can edit the file and add a new line that assigns a value to EXINIT. The definition EXINIT='set nu ai' will configure the Vi editor with line numbering and indentation. You then need to add EXINIT to the export command's argument list. When the .bash_profile file executes again, the EXINIT variable will be set to the command set nu ai. When the Vi editor is invoked, the command in the EXINIT variable will be executed, setting the line number and auto-indent options automatically.

In the following example, the user's .bash_profile has been modified to include definitions of EXINIT and redefinitions of PATH, PS1, and HISTSIZE. The PATH variable has $HOME: added to its value. $HOME is a variable that evaluates to the user's home directory, and the ending colon specifies the current working directory, enabling you to execute commands that may be located in either the home directory or the working directory. The redefinition of HISTSIZE reduces the number of history events saved, from 1,000 defined in the system's .profile file, to 30. The redefinition of the PS1 system variable changes the prompt to include the pathname of the current working directory. Any changes you make to system variables within your .bash_profile file override those made earlier by the system's .profile file. All these system variables are then exported with the export command.

.bash_profile

start example
 # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ];  then     . ~/.bashrc fi # User-specific environment and startup programs PATH=$PATH:$HOME/bin:$HOME: BASH_ENV=$HOME/.bashrc USERNAME="" HISTSIZE=30 NNTPSERVER=nntp.myserver.com EXINIT='set nu ai' PS1="\w \$" export USERNAME BASH_ENV PATH HISTSIZE EXINIT PS1 
end example

Manually Reexecuting the .bash_profile Script

Although .bash_profile is executed each time you log in, it is not automatically reexecuted after you make changes to it. The .bash_profile file is an initialization file that is executed only whenever you log in. If you want to take advantage of any changes you make to it without having to log out and log in again, you can reexecute .bash_profile with the dot (.) command. The .bash_profile file is a shell script and, like any shell script, can be executed with the . command.

$ . .bash_profile 

Alternatively, you can use the source command to execute the .bash_profile initialization file, or any initialization file such as .login used in the TCSH shell, or .bashrc.

$ source .bash_profile 

System Shell Profile Script

Your Linux system also has its own profile file that it executes whenever any user logs in. This system initialization file is simply called profile and is found in the /etc directory, /etc/profile. This file contains system variable definitions the system needs to provide for each user. A copy of the system's .profile file follows. Red Hat uses a pathmunge function to generate a directory list for the PATH variable. The path generated for the root user will look something like this, including both system and user application directories. Normal user paths will lack the system directories, those with sbin in the path, and include the name of their home directory, along with /usr/kerberos/bin for Kerberos tools.

# echo $PATH /usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11: /usr/X11R6/bin:/usr/local/bin:/usr/bin:/root/bin:/root/bin

HISTFILE is also redefined to include a larger number of history events. An entry has been added here for the NNTPSERVER variable. Normally, a news server address is a value that needs to be set for all users. Such assignments should be made in the system's /etc/profile file by the system administrator, rather than in each individual user's own .bash_profile file. The /etc/profile file also executes any scripts in the directory /etc/profile.d. This design allows for a more modular structure. Rather than make entries by editing the /etc/profile file, you can just add a script to profile.d directory. The scripts for the BASH shell have the extension .sh. For example, the kde.sh script in the profile.d directory checks for a definition of the KDEDIR variable and makes one if none is in effect.

/etc/profile

start example
 # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc pathmunge () {      if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then        if [ "$2" = "after" ] ; then           PATH=$PATH:$1      else        PATH=$1:$PATH      fi     fi }     # Path manipulation if [ 'id -u' = 0 ]; then          pathmunge /sbin          pathmunge /usr/sbin          pathmunge /usr/local/sbin fi     pathmunge /usr/X11R6/bin after unset pathmunge # No core files by default ulimit -S -c 0 > /dev/null 2>&1     USER="'id -un'" LOGNAME=$USER MAIL="/var/spool/mail/$USER" HOSTNAME='/bin/hostname' HISTSIZE=1000 NNTPSERVER=nntp.myservice.com     if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then          INPUTRC=/etc/inputrc fi     export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC export NNTPSERVER     for i in /etc/profile.d/*.sh ; do    if [ -x $i ]; then       . $i    fi done unset i 
end example

Configuring the BASH Shell: .bashrc

The .bashrc file is a configuration file executed each time you enter the BASH shell or generate any subshells. If the BASH shell is your login shell, .bashrc is executed along with your .bash_login file when you log in. If you enter the BASH shell from another shell, the .bashrc file is automatically executed, and the variable and alias definitions it contains will be defined. If you enter a different type of shell, the configuration file for that shell will be executed instead. For example, if you were to enter the TCSH shell with the tcsh command, the .tcshrc configuration file would be executed instead of .bashrc.

The User .bashrc BASH Script

The .bashrc shell configuration file is actually executed each time you generate a BASH shell, such as when you run a shell script. In other words, each time a subshell is created, the .bashrc file is executed. This has the effect of exporting any local variables or aliases you have defined in the .bashrc shell initialization file. The .bashrc file usually contains the definition of aliases and any feature variables used to turn on shell features. Aliases and feature variables are locally defined within the shell. But the .bashrc file defines them in every shell. For this reason, the .bashrc file usually holds such aliases as those defined for the rm, cp, and mv commands. The next example is a .bashrc file with many of the standard definitions:

.bashrc

start example
 # Source global definitions if [ -f /etc/bashrc ];  then     . /etc/bashrc fi set  -o ignoreeof set  -o noclobber alias rm='rm -i' alias mv='mv -i' alias cp='cp -i' 
end example

You can add any commands or definitions of your own to your .bashrc file. If you have made changes to .bashrc and you want them to take effect during your current login session, you need to reexecute the file with either the . or the source command.

$ . .bashrc 

System /etc/.bashrc BASH Script

Linux systems usually contain a system .bashrc file executed for all users. This may contain certain global aliases and features needed by all users whenever they enter a BASH shell. This is located in the /etc directory, /etc/.bashrc. A user's own .bashrc file, located in the home directory, contains commands to execute this system .bashrc file. The ./etc/bashrc command in the previous example of .bashrc does just that.

The BASH Shell Logout File: .bash_logout

The .bash_logout file is also a configuration file, which is executed when the user logs out. It is designed to perform any operations you want done whenever you log out. Instead of variable definitions, the .bash_logout file usually contains shell commands that form a kind of shutdown procedure—actions you always want taken before you log out. One common logout command is to clear the screen and then issue a farewell message.

As with .bash_profile, you can add your own shell commands to .bash_logout. In fact, the .bash_logout file is not automatically set up for you when your account is first created. You need to create it yourself, using the Vi or Emacs editor. You could then add a farewell message or other operations. In the next example, the user has a clear and an echo command in the .bash_logout file. When the user logs out, the clear command clears the screen, and then the echo command displays the message "Good-bye for now."

.bash_logout

start example
 # ~/.bash_logout clear echo "Good-bye for now" 
end example

Initialization and Configuration Files

Each type of shell has its own set of initialization and configuration files. The Bash shell configuration files were discussed previously. The TCSH shell uses .login, .tcshrc, .logout files in place of .bash_profile, .bashrc, and .bash_logout. The Z shell has several initialization files: .zshenv, .zlogin, .zprofile, .zschrc, and .zlogout. See Table 9-2 for a listing. Check the Man pages for each shell to see how they are usually configured. When you install a shell, default versions of these files are automatically placed in the users' home directories. Except for the TCSH shell, all shells use much the same syntax for variable definitions and assigning values (TCSH uses a slightly different syntax, described in its Man pages).

Table 9-2: Shell Configuration Files

BASH Shell

Function

.bash_profile

Login initialization file

.bashrc

BASH shell configuration file

.bash_logout

Logout name

.bash_history

History file

/etc/profile

System login initialization file

/etc/bashrc

System BASH shell configuration file

/etc/profile.d

Directory for specialized BASH shell configuration files

TCSH Shell

 

.login

Login initialization file

.tcshrc

TCSH shell configuration file

.logout

Logout file

Z Shell

 

.zshenv

Shell login file (first read)

.zprofile

Login initialization file

.zlogin

Shell login file

.zshrc

Z shell configuration file

.zlogout

Logout file

PDKSH Shell

 

.profile

Login initialization file

.kshrc

PDKSH shell configuration file

Configuration Directories and Files

Applications often install configuration files in a user's home directory that contain specific configuration information, which tailors the application to the needs of that particular user. This may take the form of a single configuration file that begins with a period, or a directory that contains several configuration files. The directory name will also begin with a period. For example, Mozilla installs a directory called .mozilla in the user's home directory that contains configuration files. On the other hand, the Mail application uses a single file called .mailrc to hold alias and feature settings set up by the user. Most single configuration files end in the letters rc. FTP uses a file called .netrc. Most newsreaders use a file called .newsrc. Entries in configuration files are usually set by the application, though you can usually make entries directly by editing the file. Applications have their own set of special variables to which you can define and assign values. You can list the configuration files in your home directory with the ls -a command.



 < Day Day Up > 



Red Hat(c) The Complete Reference
Red Hat Enterprise Linux & Fedora Edition (DVD): The Complete Reference
ISBN: 0072230754
EAN: 2147483647
Year: 2004
Pages: 328

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net