What are the Linux environment variables

Source: Internet
Author: User
Tags sessions

What is an environment variable

Learn or use Linux , modify environment variables are often encountered, in the end what is the environment variable? Environment variables are features of the bash shell that store information about shell sessions and the work environment. Allows data to be stored in memory for program and script access running on the shell. This data can be used to identify the user, system,Shell features, and any other data that needs to be stored.

Classification of environment variables

in the bash shell , environment variables fall into two categories:

    • Global variables

    • Local variables

Global environment variables

Global variables are visible not only to Shell sessions, but also to all shell -created child processes. To view global variables, you can use the printenv command.

$ printenv

Hostname=localhost.localhostdomain

Term=linux

Shell=/bin/bash

histsize=1000

......

Local Environment variables

Local environment variables can only be visible in the process in which they are defined. Viewing local environment variables is a bit complex, unlike global environment variables, where you can use a command to display only local environment variables. the Set command can display environment variables set for a particular process, as well as global environment variables.

$ set

Bash=/bin/bash

Bashopts=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_ Shell:progcomp:promptvars:sourcepath

Bash_aliases= ()

Bash_argc= ()

bash_argv= ()

Bash_cmds= ()

......

Positioning System Environment variables

the Linux system uses environment variables to identify itself in programs and scripts. This provides an easy way for the program to obtain system information.

When you log in to a Linux system to start a Bashshell , by default,bash looks for commands in several files called startup files. the startup file that bash checks depends on how you start the bash Shell . There are 3 ways to start the bash Shell :

    • Login As Default shell when logging in

    • Interactive Shell as a non-login shell

    • as a non-interactive shell that runs scripts

Login Shell

When you log on to a Linux system,Bashshell is started as a login shell . The login Shell reads commands from 4 different boot files. Here is the order in which the bash shell reads these files:

    • /etc/profile

    • $HOME/.bash_profile

    • $HOME/.bash_login

    • $HOME/.profile

The/etc/profile file is the primary boot file for the default bash shell on the system. This startup file is executed when each user on the system logs on. The other 3 boot files are user-specific and can be tailored to each user's needs.

/etc/profile

The/etc/profile file is the main boot file for the bash shell. As soon as you log into the Linux system,bash executes the commands in the/etc/profile file. There is a line in this file:

Export PATH USER LOGNAME MAIL HOSTNAME histsize Histcontrol

This ensures that the environment variables in the/etc/profile file are valid for all child processes created by the login shell.

There is also a for loop that accesses each file located in the/ETC/PROFILE.D directory, providing a place for Linux systems to centralize the application-specific startup files to be executed when the user logs in. Are basically related to specific applications on the system. Most apps create two boot files: one is the bash shell ( with the. Sh extension ), and the other is the C shell ( using the. CSH Extension ). For example, determine the default language character set used on the system, and then correctly set the LANG environment variable file has lang.sh and lang.csh.

$HOME startup files in the directory

these three files all play the same side: Provide users with a proprietary file to define user-specific environment variables. Most Linux distributions use only one of these 3 files:

    • $HOME/.bash_profile

    • $HOME/.bash_login

    • $HOME/.profile

these 3 files start with a point, which means that the file is hidden. They are in the user's HOME directory. the. Bash_profile Boot file checks the HOME directory for a boot file called. bashrc. If so, the startup file will execute the command in it first.

Interactive Shell

if the bash shell is not started when it is logged on to the system (for example, when a bash command is entered at the command line ), the shell being launched is called an interactive shell.

if bash is launched as an interactive shell , it does not access the/etc/profile file, but instead goes to the HOME directory to check . BASHRC is present.

The. BASHRC file has two functions: one is to view the common BASHRC files in the/etc Directory , and the other is to provide users with a place to customize their command aliases and private script functions.

the generic /etc/bashrc boot file is executed by each user who initiates an interactive shell session on the system . However , the Export command has not been implemented to make them global environment variables.

The startup file for the interactive Shell will only run each time a new interactive shell is started, so any child shell will automatically execute this interactive Shell 's startup file.

In addition,/ETC/BASHRC also executes which application-specific startup files are located in the/ETC/PROFILE.D directory.

non-interactive shell

This is the shell that the system executes the script with . Don't worry about not having a command-line prompt to run a script on your system, you still run a specific startup command.

to handle this situation,Bashshell provides the bash_env environment variable. when the shell launches a non-interactive shell , it checks the environment variable to see the startup file to be executed. If specified,the shell executes the command in the file. In some Linux distributions, this environment variable is not set by default.


This article is from the "Chang Jie listen to the Wind People" blog, please make sure to keep this source http://kevinhao.blog.51cto.com/5204735/1872709

What are the Linux environment variables

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.