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

JustHost:俄罗斯/新西伯利亚vps,512MB内存/5GB空间/不限流量/200Mbps/KVM/自由更换IP,$1.57/月

justhost怎么样?justhost是一家俄罗斯主机商,2006年成立,提供各种主机服务,vps基于kvm,有HDD和SSD硬盘两种,特色是200Mbps不限流量(之前是100Mbps,现在升级为200Mbps)。下面是HDD硬盘的KVM VPS,性价比最高,此外还有SSD硬盘的KVM VPS,价格略高。支持Paypal付款。国内建议选择新西伯利亚或者莫斯科DataLine。支持Paypal付...

腾讯云轻量服务器老用户续费优惠和老用户复购活动

继阿里云服务商推出轻量服务器后,腾讯云这两年对于轻量服务器的推广力度还是比较大的。实际上对于我们大部分网友用户来说,轻量服务器对于我们网站和一般的业务来说是绝对够用的。反而有些时候轻量服务器的带宽比CVM云服务器够大,配置也够好,更有是价格也便宜,所以对于初期的网站业务来说轻量服务器是够用的。这几天UCLOUD优刻得香港服务器稳定性不佳,于是有网友也在考虑搬迁到腾讯云服务器商家,对于轻量服务器官方...

HostMem,最新优惠促销,全场75折优惠,大硬盘VPS特价优惠,美国洛杉矶QuadraNet机房,KVM虚拟架构,KVM虚拟架构,2核2G内存240GB SSD,100Mbps带宽,27美元/年

HostMem近日发布了最新的优惠消息,全场云服务器产品一律75折优惠,美国洛杉矶QuadraNet机房,基于KVM虚拟架构,2核心2G内存240G SSD固态硬盘100Mbps带宽4TB流量,27美元/年,线路方面电信CN2 GT,联通CU移动CM,有需要美国大硬盘VPS云服务器的朋友可以关注一下。HostMem怎么样?HostMem服务器好不好?HostMem值不值得购买?HostMem是一家...

linux设置环境变量为你推荐
视频制作软件哪个好我想学做视频跟作图,推荐下什么软件好骁龙750g和765g哪个好麒麟970跟骁龙730哪个更好天气预报哪个好用哪个最准确天气预报的软件哪个比较准天气预报哪个好用哪个最准确最准天气预报软件排行是怎样的?等额本息等额本金哪个好等额本息与等额本金哪个划算?手机炒股软件哪个好手机炒股哪个软件好 要免费的行车记录仪哪个好行车记录仪哪种好用?牡丹江教育云空间登录我想知道校园云空间是用什么账号登录的?360云盘360云盘是什么?360云盘360云盘和百度云盘哪个更好
游戏服务器租用 主机测评 美国主机推荐 国外私服 512av 一点优惠网 浙江独立 免费个人空间 hostloc 鲁诺 免费私人服务器 环聊 yundun 阿里云官方网站 路由跟踪 深圳域名 买空间网 windowsserver2012r2 web服务器 qq空间打开很慢 更多