Differences between Shell environment variables and set, ENV, and export

Source: Internet
Author: User

Original article link

I. Classification of shell environment variables and differences between set env export:

 

Set: Display (SET) private variables and user variables included in Shell variables. Shell of different classes have different private variables bash, KSh, and CSH. Each shell private variable in shell is different.

Env: Display (SET) User variable

Export: Display (SET) the shell variable currently exported as the user variable.

 

For example:

[C-sharp] View plaincopy

  1. [Oracle @ zhou3 ~] $ AAA = BBB -- shell variable settings
  2. [Oracle @ zhou3 ~] $ Echo $ aaa
  3. Bbb
  4. [Oracle @ zhou3 ~] $ ENV | grep AAA -- the current user variable has not been set.
  5. [Oracle @ zhou3 ~] $Set| Grep AAA -- the shell variable has
  6. AAA = bbb
  7. [Oracle @ zhou3 ~] $ Export | grep AAA -- the specified export is not exported, and the exported variable is not.
  8. [Oracle @ zhou3 ~] $ Export AAA -- use export to export
  9. [Oracle @ zhou3 ~] $ ENV | grep AAA -- found that the user variable exists
  10. AAA = bbb

 

 

Summary: Linux Shell variables (SET), user variables (ENV), shell variables include user variables, and export is a command tool, is to display those variables that use the Export command to import user variables contained in Shell variables to user variables.

 

2. Run the unset command to clear the environment variables. Note that you can use unset to clear all the variables set by set env export.

 

[C-sharp] View plaincopy

    1. Run the unset command to clear the environment variable values. If no value is specified, the variable value is set to null. Display
    2. For example:
    3. $ Export test ="Test ..."# Add an environment variable test
    4. $ ENV | grep test # This command is input to prove that the environment variable test already exists.
    5. Test = test...
    6. $ Unset $ test # Delete the environment variable test
    7. $ ENV | grep test # This command has no output, proving that the environment variable test does not exist.

 

 

3. Use the readonly command to set read-only variables

 

[C-sharp] View plaincopy

  1. UsedReadonlyThe variable cannot be modified or cleared. Example:
  2. $ Export test ="Test ..."# Add an environment variable test
  3. $ReadonlyTest # Set the environment variable test as read-only
  4. $ Unset test # This variable cannot be deleted
  5. -Bash: unset: Test: cannot unset:ReadonlyVariable
  6. $ Test ="New"# This variable cannot be modified.
  7. -Bash: test:ReadonlyVariable

 

 

4. The most fundamental configuration and variable configuration file ~ /. Bash_profile ~ /. Bashrc ~ /. Bash_logout

~ /. The bash_profile user is read when logging on, and the commands contained are executed.

~ /. Bashrc is read when a new shell is started and executed

~ /. Bash_logout shell read when logging out

 

In addition, the shell (Bash here) initialization process is as follows:

1. BashCheck files/Etc/profileExist?

2.If yes,BashRead the file. Otherwise, skip

3. BashCheck the files in the main directory. Bash_profileWhether it exists.

4.If yes,BashRead the file. Otherwise, skip

5. BashCheck. Bash_loginWhether it exists.

6.If yes,BashRead the file. Otherwise, skip

7. BashCheck the files in the main directory. ProfileExist?

8.If yes,BashRead the file. Otherwise, skip this step.

After these steps are completed, a prompt is displayed,KshThe default prompt is$.

 

 

V. Common shell Variables

The PATH variable contains a series of directories separated by colons, from which the system looks for executable files. If the executable files you enter (such as LS, RC-update, or emerge) are not in these directories, the system will not be able to execute them (unless you enter the full path of this command, such as/bin/LS ).
The function of the rootpath variable is the same as that of the PATH variable, but it only lists the directories to be checked when the super user (Root) typed the command.
The ldpath variable contains a series of directories separated by colons. The dynamic linker searches for library files in these directories.
The manpath variable contains a series of directories separated by colons. The command man searches for man pages in these directories.
The infodir variable contains a series of directories separated by colons. The Info command searches for the info page in these directories.
The pager variable containsProgramPATH (such as less or more ).
The editor variable contains the path of the Program (file editor) that modifies the file content (such as nano or VI ).
The kdedirs variable contains a series of directories separated by colons, which contain KDE-related information.
The config_protect variable contains a series of directories separated by spaces, which are protected by portage during update.
The config_protect_mask variable contains a series of directories separated by spaces, which are not protected by portage during update.

 

 

path: determines which directories The shell uses to search for commands or programs.
Home: current user home directory
mail: refers to the mail storage directory of the current user.
shell: The shell used by the current user.
histsize: indicates the number of historical Command records saved.
LOGNAME: indicates the login name of the current user.
hostname: indicates the host name. Many applications generally obtain the host name from this environment variable if they want to use the host name.
lang/Languge: it is a language-related environment variable that can be modified by users in multiple languages.
ps1: the Basic prompt. For root users, the prompt is # And for common users, the prompt is $.
PS2: a secondary prompt. The default value is "> ". You can modify this environment variable to modify the current command line. For example, the following command will change the prompt to the string "Hello, my newprompt ".
# PS1 = "Hello, my newprompt"

Related Article

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.