Unix environment variables

Source: Internet
Author: User
Tags builtin
I. SHELL START FILES

No matter what type of shell your login shell is, each shell has its own start files. We set the commands and variables required for the ideal environment, you can write them in these start files and execute them before login. The names of these start files are:

SHELL START FILES

Sh/etc/profile
$ HOME/. profile
Bash/etc/profile
$ HOME/. profile (or $ HOME/. bashrc)
Csh None
$ HOME/. cshrc & $ HOME/. login
Tcsh None
$ HOME/. cshrc (or $ HOME/. tcshrc)
& $ HOME/. login

If these files exist, their execution sequence will be:
1. sh series/etc/profile --> $ HOME/. profile
2. csh series $ HOME/. cshrc --> $ HOME/. login

In these start files, the profile in/etc is set by the system administrator. Generally, users cannot make any changes, the. profile ,. cshrc ,. login is added or corrected, especially for csh and tcsh users. Because no start files are available in/etc, there is no environment set by system administrators, you must set all the required variables and commands on your own. Otherwise, you will often find it unfeasible. The most common example is that many commands cannot be used because they cannot be found (because no path is set), the key cannot be used (the function of the key is not defined )....

Ii. Common requirements and common instructions

When we work in a UNIX environment, some requirements are often generated to make it easier to use. These common requirements and their setting methods are as follows:

1. Hope the key can be used

Add $ HOME/. profile or $ HOME/. cshrc
Stty erase"^ H" 

2. You want to use Chinese in a UNIX environment

Add to $ HOME/. profile (sh series)
Stty-istrip cs8
LC_CTYPE = ISO_8859_1
Export LC_CTYPE
Add to $ HOME/. cshrc (csh Series)
Stty-istrip cs8
Setenv LC_CTYPE iso_8859_1

3. hope to have the doskey function like in The dossystem
This function is available only when bash and tcsh are used.

4. Hope to use the Chinese book editor (HE) on the workstation)

Add to $ home/. Profile (SH series)
Lang = big5
Export Lang
Add to $ home/. cshrc (CSH Series)
Setenv Lang big5

5. We hope that the prompt symbol will change as the path changes.

This function is not available in SH and CSH, but in bash and tcsh, the settings are as follows:
Bash: Add in $ home/. Profile
PS1 =''Pwd'>' 
Or PS1 ="W>" 
Tcsh: Add in $ home/. cshrc
Set prompt ='% ~>' 

In addition to the settings for special needs mentioned above, the commands frequently used in environment settings are as follows:

1. stty
Set the definition of input and how to output. This command has a large number of parameters, which are available.

Stty-
We can see all the parameters and their settings. Among these parameters, the common ones are:

Istrip (-istrip)
Extract all input data into 7 bits. However, the Chinese data is 8 bits, so when you need to use Chinese, add'-Istrip'This parameter is used to prevent data input from being damaged.

Cs7 cs8
Set that each field of metadata is 7 bits or 8 bits. Therefore, when you need to use Chinese characters, the following settings are generally used:
Stty-istrip cs8
Iuclc (-iuclc)
The standard input uppercase letters are treated as lowercase (or canceled ).

Olcuc (-olcuc)
Output (or cancel) uppercase or lowercase letters ).

Echo (-echo)
Output all inputs from the standard output (or cancel the output, that is, the input cannot be seen on the screen ).

In addition to these parameters, stty can also be used to define specific functions of a key, such:
Stty erase"^ H" 
Stty kill"^ U" 

2. mesg y (mesg n)
If you do not want to be disturbed by talk or other messages at work'Mesg n'Command to prevent external messages from interrupting the work ('Mesg y'Resume communication with the outside world ).

3. umask [nnn]
Set the mode of the new file or directory. The mode of the new file or directory is the attribute set by the system administrator to create minus nnn, for example, if the system is just a zombie, why should I raise a promotion? 77, and the user sets umask 022, the user's right status of each newly generated file under his/her personal account will be 755 (777-022 ).

4. Function and Alias
For a series of or a certain command, we can use the function method (in sh, ksh, bash) or alias command (in csh, tcsh, ksh, bash ), replace this or a series of commands with a short string. function usage is as follows:

Vi (){
Mesg n
/Usr/ucb/vi $ *
Mesg y
}

Where,'Vi'Is the function name, between {} is'Vi'Note that the function name cannot be the same as that of the shell internal key command (builtin). Otherwise, the function cannot be created.

For the same function, alias commands are used as follows:

Alias vi'Mesg n;/usr/ucb/vi! *; Mesg y' 

Vi is''The alias of the contained string. the alias can be the same as the builtin name of each shell (alias is also an internal key command ).

If we create a function with the same name and alias (for example, in bash) in a shell, the execution order of alias takes precedence over that of function.

3. Set common variables

Variables in UNIX environments can be divided into two types: environment variables (Enviroment Variable) and shell variables (Shell Variable). The differences between the two are as follows: environment variables do not lose their role because of shell replacement. For example, PATH is an environment variable. Therefore, even if you execute another shell after setting the PATH (for example, if sh is used, and then csh is executed in the sh Environment), the PATH environment variable still works, shell variables (such as cwd of csh) play a role only in a specific shell, and are ineffective when other shells are executed.

In the sh and csh series, there are different ways to set variables:

1. Bourne Shell Series
[VAR] = [STRING or NUMBER]
Export [VAR]

If we still need to use this variable after leaving shell and entering another program (such as tin), in addition to setting the value of this variable, we also need'Export'Command to declare, such as setting the screen type variable TERM needs'Export'.

2. C Shell Series
Set [var] = [STRING or NUMBER]
Setenv [var] [STRING or NUMBER]

Where'Set'Is to set the variable value,'Setenv'In addition to setting the variable value'Export'.

Both the Bourne Shell and C Shell have some basic common variables, such:

1. PATH

Set the path to search for executable files. If the current directory is to be included in the path to be searched, you need to give'.', Indicating that the current directory is also included in the search path, for example:

PATH =.:/bin:/usr/local/bin:/usr/ucb
Or
Set path = (./bin/usr/local/bin/usr/ucb)

2. EDITOR

Set the editor you want to use, as shown in figure
EDITOR =/usr/ucb/vi
Or
Setenv EDITOR/usr/ucb/vi

3. HOME

Set the location of the home directory of a personal account. Generally, a set value is provided when login is called/etc/passwd. Therefore, unless you have some special requirements, it is usually not reset.

4. SHELL

In the same way, login will automatically set the content of/etc/passwd. Even if you reset the content of this variable, it will not affect the use of the Account.

5. Set the reminder symbol

In sh:
PS1 ="[STRING]" 
PS2 ="[STRING]" 
In csh: set prompt ="[STRING]" 

Of course, all of the above are widely used variables. If you think this is not enough, you can use the man sh, man csh, man tcsh, and man bash commands to view the shell instructions, you can see the variables in each shell and their usage.

If you do not know the setting content of a variable'Echo $ [VAR]'To observe the content of the variable, such:

Echo $ PATH
Iv. SHELL environment changes
As mentioned above, the settings in start files will be executed in the login user, but when we want to directly change the shell used online (for example, directly execute'Csh'Or'Exec csh'At this time, do not execute start files) or change the set content. After you compile start files orScriptAfter files, you can use the following commands to update the environment without re-logging:

.[ScriptFile] (IN shell of the sh series)
Source [ScriptFiles] (in the csh series shell)

Directly execute oneScriptFile and use'.','Source'Command Execution has different effects. We can create a cd/(or cd [DIR ])ScriptFile to observe the differences between them:

BecauseScriptThe file execution environment is a set or custom subshell (you canScriptThe first line of file is similar'#! /Bin/csh'Command), so its direct execution cannot really switch the working directory of the shell environment to/, andScriptWhen file ends, subshell does not exist,'Cd/'The command effect also disappears.'.'Or'Source'Execute the command to change the working directory in the shell working environment /.ScriptThe end of file causes the effect of commands in the file to disappear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.