Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

In-depth explanation of environment variables and configuration files in CentOS

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

CentOS's environment variable profile system is a hierarchical system, which is similar to other multi-user application system configuration files, including global, user and shell, and different levels are sometimes similar to inheritance relationships.

This article will introduce the relevant contents of CentOS environment variables and configuration files in detail. Let's take a look at the detailed introduction.

What are environmental variables?

Bash shell uses a feature called environment variables (environment variable) to store information about shell sessions and work environments. That is, to allow data to be stored in memory so that scripts running in programs or shell can access them.

In bash shell, environment variables fall into two categories:

Global variable local variable

Global environment variable

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

To view global variables, you can use the env or printenv command.

[root@dev ~] # envHOSTNAME=localhostTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=10.0.100.17 56344 22SSHregions TTY printenv TERMlinux printenv TERMlinux 0USERR = root [root @ root ~] # [root@dev ~] # printenvHOSTNAME=localhostTERM=linuxSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=10.0.100.17 56344 22SSHregions TTY robot printenv TERMlinux

Use environment variables, through the $+ variable name.

[root@dev ~] # echo $HOME/root

System environment variables are basically in uppercase letters to distinguish them from those of ordinary users.

Local environment variable

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

[root@dev ~] # 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 variable

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

[root@dev shell] # sh a.sh2222 [root@dev shell] # cat a.shangxinqingbin / bashaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

What if you set the environment variable in the child shell and want to read it in the parent shell?

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

The source command (from C Shell) is a built-in command for bash shell. The dot command, which is a dot symbol (from Bourne Shell), is another name for source. Both commands take a script that will be executed as the environment of the current shell, that is, no new child process will be started. All variables set in the script will become part of the current Shell.

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

If you want to delete an environment variable

Unset var_name

Set global environment variables

As you can see above, if you want to use common environment variables in this process and child processes. It is possible to use the source command to read the same environment variable script. This is a user-defined scheme. But many times, the global environment variable we need to read does not know source, so we need a default environment variable to read the file.

When you log in to the Linux system, bash shell will start as a login shell. Logging in to shell will read from 5 different startup files

/ etc/profile$HOME/.bash_profile$HOME/.bashrc$HOME/.bash_login$HOME/.profile

/ etc/profile

The / etc/profile file is the default main startup file for bash shell. As soon as you log in to the Linux system, bash will execute the command of the / etc/profile startup file.

[root@dev shell] # cat / etc/profile# / etc/profile# System wide environment and startup programs, for login setup# Functions and aliases go in / etc/bashrc# It's NOT a good idea to change this file unless you know what you# are doing. It's much better to create a custom.sh shell script in# / etc/profile.d/ to make custom changes to your environment, as this# will prevent the need for merging in future updates.pathmunge () {case ": ${PATH}:" in *: "$1": *) if ["$2" = "after"]; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac} if [- x / usr/bin/id]; then if [- z "$EUID"] Then # ksh workaround EUID= `id-u`HISTSIZE-ru`fi USER= "`id-un`" LOGNAME=$USER MAIL= "/ var/spool/mail/$USER" fi# Path manipulationif ["$EUID" = "0"]; then pathmunge / sbin pathmunge / usr/sbin pathmunge / usr/local/sbinelse pathmunge / usr/local/sbin after pathmunge / usr/sbin after pathmunge / sbin afterfiHOSTNAME= `/ bin/hostname 2 > / dev/ null`HISTSIZE = 1000if ["$HISTCONTROL" = "ignorespace"]; then export HISTCONTROL=ignorebothelse export HISTCONTROL=ignoredupsfiexport PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL# By default, we want umask to get set. This sets it for login shell# Current threshold for system reserved uid/gids is 20 You could check uidgid reservation validity 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

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

$HOME/.bash_profile

The startup files under $HOME are user-specific startup files that define the user's environment variables. And / etc/profile is the system's environment variable for all users.

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

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

.bashrc is called through .bash _ profile.

[root@dev 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 to be set, such as JAVA_HOME, are placed in the / etc/profile.d/ directory and defined in the form of * .sh scripts.

All right, that's all of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. If you have any questions, you can leave a message and exchange. Thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report