环境变量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.

CloudCone中国新年特别套餐,洛杉矶1G内存VPS年付13.5美元起

CloudCone针对中国农历新年推出了几款特别套餐, 其中2019年前注册的用户可以以13.5美元/年的价格购买一款1G内存特价套餐,以及另外提供了两款不限制注册时间的用户可购买年付套餐。CloudCone是Quadcone旗下成立于2017年的子品牌,提供VPS及独立服务器租用,也是较早提供按小时计费VPS的商家之一,支持使用PayPal或者支付宝等付款方式。下面列出几款特别套餐配置信息。CP...

Boomer.Host(年付3.5美)休斯敦便宜VPS

Boomer.Host是一家比较新的国外主机商,虽然LEB自述 we’re now more than 2 year old,商家提供虚拟主机和VPS,其中VPS主机基于OpenVZ架构,数据中心为美国得克萨斯州休斯敦。目前,商家在LET发了两款特别促销套餐,年付最低3.5美元起,特别提醒:低价低配,且必须年付,请务必自行斟酌确定需求再入手。下面列出几款促销套餐的配置信息。CPU:1core内存:...

提速啦香港独立物理服务器E3 16G 20M 5IP 299元

提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...

linux设置环境变量为你推荐
鬼武者3内存修改器鬼武者3怎么修改985和211哪个好高校是985一般专业还是211好专业?网页传奇哪个好玩近有什么好玩的网页传奇介绍么手机杀毒哪个好手机杀毒软件哪个最好用海克斯皮肤哪个好摄魂使者薇恩和海克斯安妮皮肤哪个好 怎么合成红茶和绿茶哪个好红茶和绿茶哪个更好?东莞电信宽带东莞电信光纤宽带包月费用多少 个人公司家用宽带办理360云盘共享群360云盘怎么找共享群360云盘36t360 wifi随身 10t 有限制吗广东联通网络传真什么是网络传真(手机版)?
备案域名购买 qq空间域名 金万维动态域名 泛域名绑定 云网数据 wdcp parseerror dd444 工作站服务器 免费防火墙 昆明蜗牛家 微软服务器操作系统 512mb 如何登陆阿里云邮箱 hosts文件 卡巴斯基免费下载 俄勒冈州 华为云服务器宕机 瓦工技术 ddos攻击器 更多