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

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

Share

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

This article introduces how to set and view environment variables in Linux. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. Display the environment variable HOME

$echo $HOME

/ home/redbooks

two。 Set a new environment variable hello

$export HELLO= "Hello!"

$echo $HELLO

Hello!

3. Use the env command to display all environment variables

$env

HOSTNAME=redbooks.safe.org

PVM_RSH=/usr/bin/rsh

Shell=/bin/bash

TERM=xterm

HISTSIZE=1000

...

4. Use the set command to display all locally defined Shell variables

$set

BASH=/bin/bash

BASH_VERSINFO= ([0] = "2" [1] = "05b" [2] = "0" [3] = "1" [4] = "release" [5] = "i386-redhat-linux-gnu")

BASH_VERSION='2.05b.0 (1)-release'

COLORS=/etc/DIR_COLORS.xterm

COLUMNS=80

DIRSTACK= ()

DISPLAY=:0.0

...

5. Use the unset command to clear environment variables

Set can set the value of an environment variable. Clear the value of the environment variable with the unset command. If no value is specified, the variable value is set to NULL. Examples are as follows:

$export TEST= "Test..." # add an environment variable TEST

$env | grep TEST # this command is entered to prove that the environment variable TEST already exists

TEST=Test...

$unset $TEST # Delete the environment variable TEST

$env | grep TEST # this command has no output, which proves that the environment variable TEST already exists

6. Use the readonly command to set read-only variables

Variables cannot be modified or cleared if the readonly command is used. Examples are as follows:

$export TEST= "Test..." # add an environment variable TEST

$readonly TEST # set the environment variable TEST to read-only

$unset TEST # will find that this variable cannot be deleted

-bash: unset: TEST: cannot unset: readonly variable

$TEST= "New" # will find that this variable cannot be modified

-bash: TEST: readonly variable

The setting of the environment variable is in the / etc/profile file

If you need to add new environment variables, you can add subordinate lines

Export path=$path:/path2:/path3:/pahtN

-

Variable types of 1.Linux

Linux variables can be divided into two categories according to their lifetime:

1.1 permanent: the configuration file needs to be modified and the variable takes effect permanently.

Temporary: you can declare it with the export command, and the variable becomes invalid when the shell is closed.

two。 Three methods of setting variables

2.1 add variables to the / etc/profile file [effective for all users (permanent)]

Add a variable to the file / etc/profile file with VI, which will be valid for all users under Linux and will be "permanent".

For example: edit / etc/profile file, add CLASSPATH variable

# vi / etc/profile

Export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

Note: if you want to take effect immediately after modifying the file, you have to run # source / etc/profile, otherwise it will only take effect the next time you reenter this user.

2.2 add a variable to the. bash_profile file under the user directory [effective for a single user (permanent)]

Use VI to add variables to the. bash_profile file in the user directory, and the changes are only valid for the current user and are "permanent".

For example: edit the. bash_profile under the guok user directory (/ home/guok)

$vi / home/guok/.bash.profile

Add the following:

Export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

Note: if you want to take effect immediately after modifying the file, you have to run $source / home/guok/.bash_profile or it will only take effect the next time you reenter this user.

2.3 run the export command directly to define variables [valid only for the current shell (BASH) (temporary)]

Use [export variable name = variable value] to define a variable directly under the command line of shell. The variable is only valid under the current shell (BASH) or its child shell (BASH). When shell is closed, the variable is invalid, and there is no such variable when you open the new shell. If you need to use it, you need to redefine it.

3. View of environment variabl

3.1 use the echo command to view a single environment variable. For example:

Echo $PATH

3.2 use env to view all environment variables. For example:

Env

Use set to view all locally defined environment variables.

Unset can delete specified environment variables.

4. Commonly used environmental variables

PATH determines in which directories shell will look for commands or programs.

HOME current user home directory

Number of HISTSIZE history records

Login name of the current user of LOGNAME

HOSTNAME refers to the name of the host

SHELL current user Shell type

LANGUGE language-related environment variable, which can be modified by multiple languages

Email storage directory of the current user of MAIL

PS1 basic prompt, # for root users and $for ordinary users

On how to set and view environment variables in Linux is shared here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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