You can modify the environment variables in your system initialization files to control and adjust many aspects of your working environment. When you log in, your system reads the initialization files and uses the environment variables to configure your system. By setting the environment variables, you can customize your system to make it easier and more efficient to do your work.
This chapter describes the following tasks.
The particular initialization files responsible for your system's configuration depend on which shell the system administrator has specified as your default shell when your system was first installed. The Bourne shell is the default shell for the Solaris operating environment, but you can also use the C shell or Korn shell. Each of these shells has its own initialization file (or files).
To determine your default shell (your login shell), follow these steps.
Type echo $SHELL
.
$ echo $SHELL /bin/sh |
Review the output of the command to determine your default shell.
Refer to the following list to identify your default shell.
Regardless of the shell you are using, when you first log in your system generally runs the system profile file, /etc/profile. This file is generally owned by the system administrator and is readable (but not writable) by all users.
After your system executes the system profile, it runs the user profile. The user profile is one (or more) initialization files that define your working environment. For example, if you're in the CDE environment your system checks this file (or set of files) each time you start a new terminal or window.
Depending on which shell is set up as your default, your user profile can be one of the following:
.profile (for the Bourne and Korn shells)
.bash_profile (for the Bourne Again shell)
.login and .cshrc (for the C shell)
.tcshrc and .cshrc (for the TC shell)
.zlogin and .zshrc (for the Z shell)
Your user profile file(s) is located in your home directory and enables you to configure your working environment to your preference.
Your system sets up your system environment by using a set of specifications that are defined in the initialization files. If you want to temporarily modify your environment for the current work session, you can issue commands directly at the command prompt. However, if you want to modify your working environment on a more permanent basis, you can store “permanent” environment variables in the appropriate user profile files.
To display the environment variables that are currently set on your system, use the env command.
Type the env command and press Return:
$ env HOME=/home/user2 PATH=/usr/bin: LOGNAME=user2 HZ=100 TERM=dtterm TZ=US/Mountain SHELL=/bin/csh MAIL=/var/mail/user2 PWD=/home/user2 USER=user2 $ |
You can also use the env command to identify your login shell. It is specified in the SHELL
environment variable. In the previous example, the shell is set to /bin/csh (the C shell).
This section describes some of the more commonly used environment variables. Many of these variables might already be in your user profile. As previously mentioned, your user profile file is located in your home directory.
To view hidden (“dot”) files, use the -la options of the ls command.
The following is a partial list of environment variables that you can include in your user profile. Your current shell determines the syntax for defining environment variables.
CDPATH
– Specifies the directories to be searched when a unique directory name is typed without a full path name.
HISTORY
– Sets the number of commands available to the history command.
HOME
– Defines the absolute path to your home directory. The system uses this information to determine the directory to change to when you type the cd command with no arguments.
LANG
– Specifies the local language. Appropriate values are Japanese, German, French, Swedish, and Italian.
LOGNAME
– Defines your login name. The default for this variable is automatically set to the login name specified in the passwd database as part of the login process. See System Administration Guide: Basic Administration for information on the passwd database.
MAIL
– Specifies the path to your mailbox, which is usually located in the /var/mail/username directory, where username is your login name. See Chapter 7, Using Mail for more information on this file.
PATH
– Lists, in order, the directories that the system searches to find a program to run when you type a command. If the appropriate directory is not in the search path, you have to type it or else type the complete path name when you enter a command.
The default for this variable is automatically defined and set as specified in your user profile file as part of the login process.
PS1
– Defines your command prompt. The default prompt for the Bourne, Bourne Again, and Korn shells is the dollar sign ($). The default prompt for the C, TC, and Z shells is the percent sign (%). The default prompt for root in either shell is the pound sign (#).
SHELL
– Defines the shell that is used by vi and other tools.
TERMINFO
– Specifies the path name for an unsupported terminal that has been added to the terminfo database. You do not need to set this variable for default terminals in this database. See System Administration Guide: Advanced Administration for information on the terminfo database.
TERM
– Defines the terminal you're currently using. When you run an editor, the system searches for a file with the same name as the definition of this variable. The system first searches the path (if any) referenced by the TERMINFO
variable, and then the default directory, /usr/share/lib/terminfo, to determine the characteristics of the terminal. If a definition is not found in either location, the terminal is identified as “dumb.”
PATH
VariableThe PATH
environment variable is used to locate commands within the SunOS directory hierarchy. By setting the PATH
, you create a fixed set of directories that the system always searches whenever you type the name of a command.
For example, if you have no PATH
variable set and you want to copy a file, you need to type the full path name for the command, /usr/bin/cp. However, if you have set the PATH
variable to include the directory /usr/bin, then you can simply type cp and your system will always execute the command. This is because your system searches for the cp command in every directory that is named in the PATH
variable, and executes it when it is found. You can significantly streamline your work by using the PATH
variable to list the commonly used SunOS
command directories.
For the Bourne, Bourne Again, and Korn shells, you can specify the PATH
variable in your user profile file (in your home directory) by using the following syntax.
PATH=.:/usr/bin:/home/bin |
In the previous example, home represents the path name of your home directory.
For the C, TC, and Z shells, you can specify the PATH
variable in your user profile file (in your home directory) by using the following syntax:
set path=(/usr/bin home/bin .) |
In this command, home is the path name of your home directory.
In the C, Korn, TC, and Z shells you can use the shortcut ~ to represent the path name of your home directory.
If you modify the PATH
variable, and you are running the C, TC, or Z shell, use the source command to make the changes effective in your current window without having to log out:
example% source user-profile-file |
If you are running the Bourne, Bourne Again, or Korn shell, type the following to make the changes effective in your current window without having to log out:
$ . user-profile-file |
Command aliases are helpful shortcuts for commands you often type. For example, the default setting for the remove command (rm) does not ask for confirmation before it removes files. Sometimes this default is inconvenient, as a typing error can remove the wrong file. However, you can use the alias variable to change this setting by editing your user profile file.
In the C and TC shells, add the following line to your user profile file.
alias rm 'rm -i' |
In the Bourne Again, Korn, and Z shells, add the following line to your user profile file.
alias rm='rm -i' |
With this line in your user profile file, typing rm is now the same as typing rm -i, which is the interactive form of the rm command. You will then always be asked to confirm the command before any files are deleted. The quote marks around rm -i in the previous example are necessary to include the blank space between rm and -i. Without the quotation marks, the shell cannot correctly interpret the text after the space.
To make your changes to your user profile file effective immediately in your current window, you need to type an additional command. In the C and TC shells, type the following command to make your alias effective immediately.
example% source user-profile-file |
The source command causes the system to read the current user profile file and execute the commands in this file.
In the Bourne Again, Korn, and Z shells, type the following command to make your alias effective immediately.
$ . user-profile-file |
In the Bourne Again, Korn, and Z shells, the . command performs the same actions as the source command in the C and TC shells.
Command aliases that are created by using the alias command apply only to the current session.
The syntax you use to change your command prompt depends on what shell you are using.
In the Bourne, Bourne Again, Korn, and Z shells, use the PS1 command to redefine your command prompt. The following are three examples:
To set the prompt to a colon (:), followed by a space, type the following command.
PS1=": " |
To create a prompt that consists of your machine name, followed by a colon and a space, type the following command.
PS1="`hostname`: " |
To set the prompt to your machine name, followed by your login name in braces {}, a colon, and a space, type the following command
PS1="`hostname`{`logname`}: " |
Type any of the previous examples to change your current command prompt. This change applies until you change your command prompt again or log out.
If you want to make your changes more permanent, add one of the previous examples (or a prompt of your own creation) to your user profile file. If you follow this guideline, the prompt you specify appears each time you log in in or start a new shell.
For the C and TC shells, you personalize your command prompt with the set prompt command. The following are three examples:
To set the prompt to a percent sign followed by a space, type the following command.
example% set prompt="% " |
To create a prompt that consists of your machine name, followed by the history number of the command (hostname1, hostname2, hostname3, and so on), followed by a colon, type the following command.
example% set prompt="`hostname`\!: " |
To set the prompt to your machine name, followed by your login name in braces, a colon, and a space, type the following command.
example% set prompt="`hostname`{`logname`}: " |
Type any of the previous examples to change your current command prompt. This change applies until you change your command prompt again or log out.
If you want to make your changes more permanent, add one of the previous examples (or a prompt of your own creation) to your user profile file. If you follow this procedure, the prompt you specify appears each time you log in or start a new shell.
You can set many other variables in your user profile file. For a complete list, refer to the man Pages(1): User Commands. The following sections describe some of the more commonly used options.
noclobber
VariableUse set noclobber to prevent unintentional overwriting of files when you use the cp command to copy a file. This variable affects the Bourne Again, C, Korn, and TC shells. Type the following in your user profile file:
set noclobber |
history
VariableThe history
variable enables you to set the number of commands that you saved in your history list. The history command is useful to view commands you have previously typed. You can also use the history file to repeat earlier commands. Type the following in your .cshrc or .tcshrc file:
set history=100 |
You can also affect the Bourne, Bourne Again, Korn, and Z shells in the same manner by typing the following line in your user profile file.
HISTORY=100 |
The umask command sets a default file permission for all the files and directories you create. For example, if you are security conscious and you want to grant members of your group, and all users, only read and execute permissions (-rwxr-xr-x) on your directories and files, you can set the umask in your user profile file so that every new file or directory you create is protected with these permissions.
Like the chmod command, umask uses a numeric code to represent absolute file permissions. However, the method that is used to calculate the code for umask is different from the method for chmod.
For example, if umask is set to 000, all files you create have the following (read and write, but not execute) permissions:
rw-rw-rw- (mode 666)
All directories that are created have the following (read, write, and execute) permissions:
rwxrwxrwx (mode 777)
To determine the value to use for umask, you subtract the value of the permissions you want (using the value you would specify for the chmod command) from the current default permissions assigned to files. The remainder is the value to use for the umask command.
For example, suppose you want to change the default mode for files from 666 (rw-rw-rw-) to 644 (rw-r--r--). Subtract 644 from 666. The remainder, 022, is the numeric value you would use with umask as follows:
$ umask 022 |
Similar to the numeric code for the chmod command, the three numbers that are used with umask are as follows.
The first digit controls user permissions
The second controls group permissions
The third digit controls permissions for all others
Table 10-1 shows the file permissions that are created for each digit of the umask command's numeric code.
Table 10–1 Permissions for umask
umask code |
Permissions |
---|---|
0 |
rwx |
1 |
rw- |
2 |
r-x |
3 |
r-- |
4 |
-wx |
5 |
-w- |
6 |
--x |
7 |
---(none) |
For more information on the umask command, refer to the man Pages(1): User Commands.