环境变量linux环境变量设置--总结(Linux environment variable settings -- Summary)

linux设置环境变量  时间:2021-01-27  阅读:()

linux环境变量设置--总结Linux environment variable settings

-- Summary

Linux environment variable settings -- Summary

1. Introduction

In the Linux system, if you download and install an application,it is likely that the name of "command not found" appears whentyping its name. It' s too tedious to find executable files everytime you go to the installation target folder. This involvessetting up the environment variable PATH, and the setting ofPATH is also an integral part of customizing the environmentvariables in linux. Based on RedHat 9, this paper gives adetailed explanation of the customization of environmentv ar i ab l e s.

2. Brief introduction of variables

Linux is a multi-user operating system. After each user loginsystem, there will be a dedicated running environment.Typically, the default environment for each user is the same,and the default environment is actually a set of definitionsof the environment variables. Users can customize theiroperating environment by modifying the corresponding systemenvironment variables.

3. Customizing environment variables

The environment variable is closely related to Shell, and aShell is started after the user logs into the system. ForLinux,

it is generally bash, but it can also be reset or switched toother Shell. According to the release version, bash has twobasic system level configuration files: /etc/bashrc and/etc/profile. These configuration files contain two differentsets of variables: shell variables and environment variables.The former is fixed only in a particular shell (such as bash) ,and the latter is fixed in different shell. It is obvious thatthe shell variable is local, and the environment variable isglobal. The environment variables are set by the Shell command,and the set environment variables can be used by all theprograms that the current user runs. For the bash Shell program,you can access the corresponding environment variables byvariable names, and set the environment variables throughexport. The following is illustrated by several examples.

3. 1 use command echo to display environment variables-----------------------------------------------------------

This example uses the echo # display common variable HOME$e c h o $HOM E

/home/l qm

-----------------------------------------------------------

---------------------

3.2 set a new environment variable

-----------------------------------------------------------

---------------------

$export HELLO= "Hello"!"

$echo $HELLO

Hello!

-----------------------------------------------------------

---------------------

3.3 use the env command to display all the environment variables-----------------------------------------------------------

---------------------

$env

S SH_AGENT_P I D=1875

HOSTNAME=lqm

SHELL=/bin/bash

TERM=xt erm

HISTSIZE=1000

. . . . . .

-----------------------------------------------------------

---------------------

3.4 use the set command to display all locally defined Shellv ar i ab l e s

-----------------------------------------------------------

---------------------

$set

BASH=/bin/bash

. . . . . .

-----------------------------------------------------------

---------------------

3.5 use the unset command to clear the environment variables-----------------------------------------------------------

---------------------

$export TEST= "test" # added an environment variable TEST$env grep TEST | # this command output, prove the environmentvariable TEST already exists

TEST=test

$unset $TEST # delete environment variable TEST

$env grep TEST | # this command without output, prove theenvironment variable TEST already exists

-----------------------------------------------------------

3.6 set read-only variables using the readonly commandIf the readonly command is used, the variables cannot bemodified or cleared. Examples are as follows:

-----------------------------------------------------------

---------------------

$export TEST= "Test. . . " # added an environment variable TEST$readonly TEST # set the environment variable TEST read-only.$unset TEST # will find this variable cannot be deleted-bash: unset: TEST: cannot unset: readonly variable

$TEST= "New" #will find this variable cannot be modified-bash: TEST: readonly variable

-----------------------------------------------------------

---------------------

3.7 use C programs to access and set environment variables

For users of C programs, you can use the following threefunctions to set up or access an environment variable.Getenv () access an environment variable. The input parameteris the name of the variable to be accessed, and the return valueis a string. If the access environment variable does not exist,it returns NULL.

Setenv () the function of setting up an environment variablein the program.

Unsetenv () a function that clears a particular environmentv ar i ab l e.

In addition, there is apointer variable, environ, which refersto a list containing all of the environment variables. Thefollowing program can print all the environment variables inthe current running environment:

-----------------------------------------------------------

---------------------

#include <stdio.h>

Extern char**environ;

Int main ()

{

Char**v ar;

For (VaR =environ; *var! =NULL; ++var)

P r i n t f ("%s \n", *v ar) ;

Return 0;

}

-----------------------------------------------------------

---------------------

3.8 modify the environment variable by modifying theenvironment variable definition file.

It is important to note that, in general, this is onlyapplicable to ordinary users, and avoid modifying the rootuser' s environment definition file, which may pose a potentialdanger.

-----------------------------------------------------------

---------------------

$cd # to users under the root directory

$ls -a # view all files, including hidden files

$vi.Bash_profile # modify environment variables definitionfile

-----------------------------------------------------------

---------------------

Then edit your PATH statement, which is formatted as:PATH=$PATH:<PATH 1>: <PATH 2>: <PATH 3>: : <PATH N>

You can add the specified path by itself, separated by colons.When the environment variable is changed, it will take effectwhen the user enters the next time. If you want to take effectimmediately, you can execute the following statement:$source.Bash_profile

Note that it is best not to the current path"/" in PATH, thismay be an unexpected attack. After completion, you can view thecurrent search path through $echo $PATH. With thiscustomization, you can avoid frequent programs starting fromthe path outside the shell search.

4 Summary

Through the above settings,

You can have a more convenient and effective environment toimprove your work efficiency.

BuyVM迈阿密KVM上线,AMD Ryzen 3900X+NVMe硬盘$2/月起

BuyVM在昨天宣布上线了第四个数据中心产品:迈阿密,基于KVM架构的VPS主机,采用AMD Ryzen 3900X CPU,DDR4内存,NVMe硬盘,1Gbps带宽,不限制流量方式,最低$2/月起,支持Linux或者Windows操作系统。这是一家成立于2010年的国外主机商,提供基于KVM架构的VPS产品,数据中心除了新上的迈阿密外还包括美国拉斯维加斯、新泽西和卢森堡等,主机均为1Gbps带...

DogYun(300元/月),韩国独立服务器,E5/SSD+NVMe

DogYun(中文名称狗云)新上了一批韩国自动化上架独立服务器,使用月减200元优惠码后仅需每月300元,双E5 CPU,SSD+NVMe高性能硬盘,支持安装Linux或者Windows操作系统,下单自动化上架。这是一家成立于2019年的国人主机商,提供VPS和独立服务器租用等产品,数据中心包括中国香港、美国洛杉矶、日本、韩国、德国、荷兰等。下面分享这款自动化上架韩国独立服务器的配置和优惠码信息。...

香港站群多ip服务器多少钱?零途云香港站群云服务器怎么样?

香港站群多ip服务器多少钱?想做好站群的SEO优化,最好给每个网站都分配一个独立IP,这样每个网站之间才不会受到影响。对做站群的站长来说,租用一家性价比高且提供多IP的香港多ip站群服务器很有必要。零途云推出的香港多ip站群云服务器多达256个IP,可以满足站群的优化需求,而且性价比非常高。那么,香港多ip站群云服务器价格多少钱一个月?选择什么样的香港多IP站群云服务器比较好呢?今天,小编带大家一...

linux设置环境变量为你推荐
985和211哪个好985大学好 还是211是什么意思朗逸和速腾哪个好大众速腾与朗逸哪个好音乐播放器哪个好音乐播放器哪个最好用清理手机垃圾软件哪个好清理手机垃圾文件的软件哪个好?腾讯空间登录腾讯qq空间进入登陆个人QQ空间qq空间登录不上为什么我的qq空间登不上去qq空间登录电脑手机上怎么登陆电脑版QQ空间yy空间登录怎么进入YY音乐空间辽宁联通网上营业厅网联通宽带好不好用YunOSYunOS怎么样,有用过的吗?
万网域名查询 香港服务器租用 韩国vps俄罗斯美女 淘宝二级域名 12u机柜尺寸 网通代理服务器 ibrs e蜗 qq对话框 免费申请网站 我的世界服务器ip 免费asp空间 华为云建站 深圳主机托管 phpinfo 中美互联网论坛 德国代理 神棍节 遨游论坛 赵蓉 更多