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

The method of setting environment variable by Linux

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

Share

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

This article mainly explains "the method of setting environment variables in Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Linux's method of setting environment variables".

Linux is a multi-user operating system. Multi-user means that after each user logs in to the system, he or she has his own dedicated running environment. This environment is defined by a set of variables, which are called environment variables. Users can modify their environment variables to meet the requirements of the environment.

The method of setting environment variables

Permanent variables that are valid for all users

Such variables take effect for all users in the system, and all users can use them. The scope of action is the whole system.

This file can only be modified under root.

# vi / etc/profile export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib

The new environment variable will not take effect immediately after it is added, and you need to run source / etc/profile immediately, otherwise it will only take effect the next time you re-enter the user.

Permanent variables that are valid for a single user

Add variables to the. bash_profile file under the user directory, which is a hidden file and can be viewed using ll-a

$whoami rethink $vi / home/rethink/.bash_profile export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib $source / home/rethink/.bash_profile

In the above figure, two files are framed in red: .bashrc and .bash _ profile. In principle, when setting such environment variables, it is possible to add to either of these two files. The difference between these two files is: .bash_profile is interactive login mode into bash shell operation, .bashrc is interactive non-login mode into bash shell operation.

It can be understood that the .bash _ profile file is read only once when the user logs in, while .bashrc reads every time the terminal is opened for a new session.

Temporarily valid environment variables (valid for the current shell only)

Such environment variables are only valid for the current shell. This environment variable disappears when we log out or close the terminal and reopen it. It's temporary.

Setting method: use [export variable name = variable value] to define the variable directly under the command line.

$export NAME= "rethink" $echo $NAME rethink

Common commands for setting environment variables

Echo is used to print and display environment variables, such as: echo $NAME

Export is used to set new environment variables, such as export NAME='rethink'

Update environment variables update environment variables can be directly re-assigned: NAME='test' (note: no need to add $before the variable name)

Env displays variables for the current user

Set displays the current shell variable, and the shell variable contains user variables

Unset deletes an environment variable, such as unset NAME

Readonly sets the environment variable to read-only, such as readonly NAME. The read-only variable unset is invalid.

Common environmental variables

PATH # echo $PATH / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

The paths are separated by colons, which are lists of directories where executable programs can be found. When we enter an instruction, shell first checks whether the command is an internal command, and if not, it checks to see if the command is an application, and shell tries to find these applications in PATH.

If shell does not find the executable in these path directories, it will report an error; if so, the system will call to execute the application. By setting PATH, we can make it easier for us to run programs or instructions.

To add a directory path to PATH, you can write:

$pwd / root/docker/httpd $export PATH=$PATH:$PWD $echo $PATH / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/docker/httpd `you can see that the path of our current directory has been added after PATH`

HOME

The user's home working directory, which is the default directory when the user logs in to the Linux system.

$whoami rethink $echo $HOME / home/rethink

HISTSIZE

The number of commands that save history. All the instructions we enter will be saved by the system, and this environment variable records the number of instructions to be held. It's usually 1000.

$echo $HISTSIZE 1000$ HISTSIZE=1001$ echo $HISTSIZE 1001

History commands are saved in memory and are automatically saved or read when you exit or log in to shell. We can view them through the history command. You can use symbols! Executes the history command of the specified sequence number. For example, to execute the second history command, enter! 2.

$history 5 59 ls 60 who 61 history | head-n 5 62 who am i 63 history 5 $! 62 who am i root pts/0 2018-04-04 11:00 (101.81.92.18)

LOGNAME

The current user login name.

$echo $LOGNAME rethink

HOSTNAME host name.

$echo $HOSTNAME JDu4e00u53f7

SHELL

The type of shell used by the current user.

$echo $SHELL / bin/bash so far, I believe you have a deeper understanding of "the method of setting environment variables in Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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