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

How to set and view environment variables in Linux system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to set and view the environmental variables in the Linux system, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Environment variable

Environment variables are implemented in the form of key-value pairs, are available system-wide and are inherited by all derived child processes and Shell. The names of environment variables are case-sensitive and are usually named in uppercase (MYVAR1, MYVAR2... )

The environment variable for a single value looks like this:

KEY=value1

If you want to assign multiple values to an environment variable, you usually use a colon (:) as the delimiter. Each key-value pair ends up looking like this:

KEY=value1:value2:value3

If you want to assign spaces to the value of the environment variable, you need to use quotation marks:

KEY= "value with spaces"

Shell variable

Shell variables are variables in the Shell that are specifically used to set or define them. Each Shell, such as zsh and bash, has its own internal set of Shell variables. They are often used to track temporary data, such as the current working directory, and the rules are the same as environment variables.

If you want to use the Shell variable as a global variable, you can use the export directive:

$export MYVAR=lxlinux.net $echo $MYVAR lxlinux.net $env | grep MYVAR MYVAR=lxlinux.net

Common environment variables and Shell variables

Some environment variables and Shell variables are very useful and are often referenced. Here are some common environment variables that you may encounter later:

The variable name means TERM, which specifies the type of terminal to simulate when running Shell. Different hardware terminals can be simulated according to different operation requirements. However, you usually don't need to care about this variable. The user currently logged in to USER, PWD, is the current working directory on OLDPWD. This variable is saved by Shell so that you can switch back to the previous working directory by executing cd -. LS_COLORS this defines the color output code of the ls instruction, which is used to add color output to the ls instruction. This is often used to distinguish different file types and to make information such as file types clear to users at a glance. The path to the mailbox of the current user MAIL the list of directories that the PATH system checks when looking for instructions. When the user enters an instruction, the system checks the directory in the order of the directory list to find the corresponding executable. LANG's current language and localization settings, including character encoding. The last instruction executed on the home directory of the current user of HOME

In addition to the above environment variables, you may also encounter the following Shell variables frequently:

The variable name refers to the list of options enabled when BASHOPTS executes bash, which is helpful in determining whether the Shell environment is running as expected. The executing bash version of BASH_VERSINFO machine-readable format of BASH_VERSION human readable format the executing bash version of COLUMNS is used to set the wide number of columns of output information drawn to the screen and the directory stack available for the popd command. The number of lines of command history that HISTFILESIZE stores in the file. The default is the number of lines in the ~ / .bash_history file. The number of lines of command history allowed to be stored in HISTSIZE memory, that is, the number of lines that can be printed by the histroy command. The hostname of the HOSTNAME computer, the IFS internal field delimiter, used to separate input on the command line. Spaces are used as delimiters by default. PS1 defines the main command prompt. This is used to define the appearance of the command prompt when starting a Shell session. PS2, on the other hand, is used to declare a command prompt for commands that span multiple lines. SHELLOPTS can set the Shell option with the set command. UID current user's UID (user ID)

View Shell variables and environment variables

On the Linux system, there are several commands that let you view environment variables:

Env-this command allows you to run programs in a custom environment without changing the current environment. When you use the env command without arguments, it prints out the current list of environment variables.

Printenv-can print out all or specified environment variables.

Set-this command sets or removes the Shell variable. When you use the set command without arguments, it prints out a list of all variables, including environment variables and Shell variables, as well as the Shell function.

By default, env and printenv function exactly the same:

$printenv SSH_CONNECTION=10.0.2.2 37182 10.0.2.15 22 LESSCLOSE=/usr/bin/lesspipe% s LANG=C.UTF-8 XDG_SESSION_ID=5 USER=alvin MYVAR=lxlinux.net PWD=/home/alvin HOME=/home/alvin SSH_CLIENT=10.0.2.2 37182 22 XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop SSH_TTY=/dev/pts/0 MAIL=/var/mail/alvin TERM=xterm-256color SHELL=/bin/bash SHLVL=1 LOGNAME=alvin XDG_RUNTIME _ DIR=/run/user/1000 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin LESSOPEN= | / usr/bin/lesspipe% s _ = / usr/bin/printenv

Only in more specific functions can the difference between the env command and the printenv command be reflected. For example, using the printenv command, you can request the value of a single variable:

$printenv SHELL / bin/bash $printenv HOME / home/alvin $printenv MYVAR lxlinux.net

The env command modifies the environment in which the program runs by passing a set of variables to the command:

Env MYVAR=lxlinux.net command_to_run command_options

The printenv and env commands can only print out environment variables, and if you want to print out a list of all variables or Shell functions, you can use the set command.

$set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES= () BASH_ARGC= () BASH_ARGV= () BASH_CMDS= () BASH_COMPLETION_VERSINFO= ([0] = "2" [1] = "8") BASH_LINENO= () BASH_SOURCE= () BASH_VERSINFO= ([0] = "4" [1] = "4" [2] = "20" "[3] =" 1 "[4] =" release "[5] =" x86_64-pc-linux-gnu ") BASH_VERSION='4.4.20 (1)-release' COLUMNS=140 DIRSTACK= () EUID=1000 GROUPS= () HISTCONTROL=ignoreboth HISTFILE=/home/alvin/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 HOME=/home/alvin HOSTNAME=ubuntu-bionic HOSTTYPE=x86_64 IFS=$'\ t\ n'LANG=C.UTF-8 LESSCLOSE='/usr/bin/lesspipe% s% s' LESSOPEN=' | / usr/bin/lesspipe% s' LINES=35 LOGNAME=alvin.

This command will display a large list of all variables, so you may want to pass the output to the less command.

$set | less

Set Shell variables and environment variables

There are several commands in the Linux system that can set environment variables:

Set-this command can set or unset the Shell variable. When you use the set command without arguments, it prints out a list of all variables, including environment variables and Shell variables, as well as the Shell function.

Unset-this command removes Shell variables as well as environment variables.

Export-this command sets the environment variable.

To better understand the difference between Shell variables and environment variables, let's start with setting Shell variables and then talk about environment variables.

Start by defining a Shell variable in the current session:

$MYVAR=lxlinux

You can use echo $MYVAR to verify that the variable is set:

$echo $MYVAR lxlinux

Use the printenv command to verify that the variable is an environment variable:

$printenv MYVAR

No output is returned, which means that the MYVAR variable is not an environment variable.

The export command can be used to set environment variables. To create an environment variable, simply export the Shell variable as an environment variable with the export command:

$export MYVAR

You can use the following sentence to check:

$printenv MYVAR lxlinux

Of course, you can also set environment variables with just one line of code:

$export MYNEWVAR= "My New Variable"

However, environment variables created in this way can only be used in the current session. If you open a new Shell session or log out, all variables will be lost.

We can also restore the environment variable to the Shell variable, or delete it completely:

The MYVAR variable is defined as the environment variable, and we can restore it to the Shell variable by typing the following code:

$export-n MYVAR

In this way, the MYVAR variable is no longer an environment variable, but it is still a Shell variable.

Whether it is a Shell variable or an environment variable, if you want to delete the variable completely, you can use the unset command to delete:

$unset MYVAR

You can verify that the MYVAR variable has been deleted by the following statement:

$echo $MYVAR

Because the variable has been deleted, there is no return.

Persistence of environmental variables

Many programs need to use environment variables to determine how they are executed, but we don't want to have to reset important variables every time we start a new Shell session, so we need to write important environment variables to the configuration file.

Shell sessions can be started in different ways, such as interactive Shell connected to the terminal and non-interactive Shell not connected to the terminal, login Shell and non-login Shell, and bash Shell reads different configuration files according to how the session is started.

However, in most Linux distributions, when you start a new Shell session, you typically read the environment variables from the following files:

/ etc/environment-use this file to set system-wide environment variables.

/ etc/profile-whenever bash logs in to Shell, the variables set in this file are loaded.

~ / .bashrc-the Shell profile specific to each user. For example, if you are using Bash, you can declare variables in it.

If you want to load new environment variables into the current Shell session, you can use the source command:

$source ~ / .bashrc

If you want to set environment variables, you can consider adding them to the / etc/profile, / etc/bash.bashrc, or / etc/environment files.

Thank you for reading this article carefully. I hope the article "how to set and view the environmental variables in the Linux system" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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