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

Source: Internet
Author: User
Shell Environment Variable Classification and set env export differences:

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:

[Www.linuxidc.com @ linuxidc ~] $ AAA = BBB-shell variable settings
[Www.linuxidc.com @ linuxidc ~] $ Echo $ aaa
Bbb
[Www.linuxidc.com @ linuxidc ~] $ ENV | grep AAA-the current user variable has not been set
[Www.linuxidc.com @ linuxidc ~] $ Set | grep AAA-shell variable has
AAA = bbb
[Www.linuxidc.com @ linuxidc ~] $ Export | grep AAA-the specified export is not exported, and the exported variable is not.
[Www.linuxidc.com @ linuxidc ~] $ Export AAA-Use Export to export
[Www.linuxidc.com @ linuxidc ~] $ ENV | grep AAA-found that the user variable exists
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.

Environment variable configuration file

The most fundamental configuration file for setting and changing variables ~ /. 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

Differences between. bashrc and. bash_profile

Reference

. Bash_profile will be used in Login Shell
. Bashrc is used in interactive non-Login Shell

In bash, each user can configure two initial files:. bash_profile and. bashrc, which are stored in ~ In the root directory. Man Bash:

,--------------------------
| ~ /. Bash_profile
| The personal initialization file, executed for login shells
| ~ /. Bashrc
| The individual per-interactive-shell Startup File
'--------------------------

  • Execute. bash_profile every time Bash is started as a login shell.

  • Every time Bash is started as a normal Interactive Shell (Interactive Shell), it will execute. bashrc

  • Note:
    1) in the shell script, "#! The bash started by/usr/bin/Bash does not execute. bashrc. This is because Bash is not interactive shell.

2) Bash is not executed when it is started as a login shell (LOGIN bash). bashrc. Although the shell is also interactive shell, it is not a common shell.

  • Generally. bash_profile will call. bashrc

Although. bashrc is not automatically executed when login Bash is started, it explicitly calls. bashrc in. bash_profile. Therefore, in your. bash_profile file, you may see the following code snippet:
[Plain] view plaincopy view the code piece derived from my code piece on the code
If [-f ~ /. Bashrc]; then

. ~/.bashrc

Fi

. Bashrc is used in interactive non-login shell. It means that every time you run a bash script,. bashrc will be executed once. You can use echo to print something in. bash_profile and. bashrc ., We can see when the two files were executed.

Edit/etc/profile to modify global environment variables
Edit. bash_profile to modify the environment variable of the current user
After the modification is complete, the source will take effect immediately, for example, source ~ /. Bash_profile

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

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.