Environment variables and configuration files in CentOS

Source: Internet
Author: User

What is an environment variable

Bash Shell uses a feature called environment variable (environment variable) to store information about shell sessions and the work environment. This allows data to be stored in memory so that scripts running in the program or shell can access them.

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

    • Global variables
    • Local variables
Global environment variables

Global environment variables are visible to shell sessions and all spawned child shells. Local variables are only visible to the shell that created them.

View global variables, which can be used env or printenv commands.

[[email protected] ~]# envHOSTNAME=localhostTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=10.0.100.17 56344 22SSH_TTY=/dev/pts/0USER=root[[email protected] ~]# [[email protected] ~]# printenvHOSTNAME=localhostTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=10.0.100.17 56344 22SSH_TTY=/dev/pts/0USER=root[[email protected] ~]# printenv TERMlinux

Using environment variables, pass the $ + variable name.

[[email protected] ~]# echo $HOME/root

System environment variables are basically the use of uppercase letters to distinguish the environment variables from ordinary users.

Local Environment variables

As the name implies, local environment variables can only be visible in the process in which they are defined. setthe command displays all environment variables that are set by a particular process, including local variables, global variables, and user-defined variables.

[[email protected] ~]# setBASH=/bin/bashBASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepathBASH_ALIASES=()BASH_ARGC=()BASH_ARGV=()BASH_CMDS=()BASH_LINENO=()BASH_SOURCE=()BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")BASH_VERSION='4.1.2(1)-release'COLORS=/etc/DIR_COLORSCOLUMNS=165
User-defined variables

Once you launch the bash shell, you can create local variables that are visible within the shell process. The child shell created by the process cannot read the local variables of the parent shell.

[[email protected] shell]# sh a.sh2222[[email protected] shell]# cat a.sh#!/bin/basha=1;export b=2;sh b.shecho $b;[[email protected] shell]# cat b.sh#!/bin/bashecho $a;echo $b;b=22;echo $b;[[email protected] shell]# sh a.sh 2222

The user can export make the variable into a global variable through a variable so that the child shell can also read it. The child shell modifies the variable and is not affected in the parent shell .

If you set environment variables in a child shell, do you want to read them in the parent shell?

One usage scenario is that multiple execution scripts rely on a common environment configuration, which is written in a env.sh script, how can other execution scripts be read into env.sh variables? The export variable in the child shell does not affect the parent shell.

The Source command (from C Shell) is a built-in command of the bash shell. The point command, which is a point symbol, is another name for source (from the Bourne shell). Both commands are parameterized with a script that executes as the current shell's environment, that is, a new child process is not started. All variables set in the script will become part of the current Shell .

[[email protected] shell]# cat c.sh. ./env.shsource ./profile.shecho $env;echo $profile;[[email protected] shell]# cat env.shenv='test';[[email protected] shell]# cat profile.sh profile="dev";[[email protected] shell]# sh c.sh testdev

If you want to delete an environment variable

unset var_name
Setting Global environment variables

As you can see above, if you want to use common environment variables in this and child processes. sourcea script that reads the same environment variable by command can be implemented. This is a user-defined scenario. But many times we need to read the global environment variable without knowing the source, so a default environment variable is required to read the file.

When you log in to a Linux system, the Bash shell is launched as a login shell. Login shell will be read from 5 different boot files

    • /etc/profile
    • $HOME/.bash_profile
    • $HOME/.BASHRC
    • $HOME/.bash_login
    • $HOME/.profile
/etc/profile

/etc/profileFile is the default master boot file for bash shell. As soon as you log in to the Linux system, bash executes the/etc/profile boot file command.

[[email protected] shell]# cat/etc/profile#/etc/profile# System wide environment and startup programs, for login SE tup# Functions and aliases go in/etc/bashrc# It's not a good idea to change this file unless what know is you# . It ' s much better to create a custom.sh shell script in#/etc/profile.d/to do custom changes to your environment, as thi S# would prevent the need for merging in future Updates.pathmunge () {case ': ${path}: ' in *: ' $ ':*);        ; *) If ["$" = "after"]; Then path= $PATH: $ else path=$1: $PATH fi esac}if [-x/usr/bin/id] ; Then if [-Z "$EUID"]; Then # Ksh workaround euid= ' id-u ' uid= ' Id-ru ' fi user= "' Id-un '" logname= $USER mail= "/V ar/spool/mail/$USER "fi# Path manipulationif [" $EUID "=" 0 "]; Then Pathmunge/sbin pathmunge/usr/sbin pathmunge/usr/local/sbinelse pathmunge/usr/local/sbin after path Munge /usr/sbin after Pathmunge/sbin afterfihostname= '/bin/hostname 2>/dev/null ' histsize=1000if ["$HISTCONTROL" = "Igno Respace "]; Then export histcontrol=ignorebothelse export histcontrol=ignoredupsfiexport PATH USER LOGNAME MAIL HOSTNAME histsiz E histcontrol# By default, we want Umask to get set. This sets it for login shell# current threshold for system reserved Uid/gids are 200# you could check uidgid reservation VA Lidity in#/usr/share/doc/setup-*/uidgid Fileif [$UID-gt 199] && ["' id-gn '" = "' Id-un '"]; Then umask 002else umask 022fifor i in/etc/profile.d/*.sh; do if [-R "$i"]; Then if ["${-#*i}"! = "$-"]; Then. "$i" else. "$i" >/dev/null 2>&1 fi fidoneunset iunset-f Pathmunge

The file reads /etc/profile.d/ all the files under it *.sh and loads the variables through the Point command (source). That is, variables defined in/etc/profile and/etc/profile.d/*.sh are global system environment variables.

$HOME/.bash_profile

The startup file under $HOME is a user-specific startup file that defines the user's environment variables. And/etc/profile is the system, all user's environment variable.

The shell runs the first found file in the following order, and the rest is ignored:

    • $HOME/.bash_profile
    • $HOME/.bash_login
    • $HOME/.profile

The. BASHRC is called through. Bash_profile.

[[email protected] shell]# cat  ~/.bash_profile # .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then        . ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/binexport PATH

Summary :

The system global environment variables that will be set, such as Java_home, are placed in the/etc/profile.d/directory and are defined as *.sh scripts.

Environment variables and configuration files in CentOS

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.