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

SugarHosts糖果主机六折 云服务器五折

也有在上个月介绍到糖果主机商12周年的促销活动,我有看到不少的朋友还是选择他们家的香港虚拟主机和美国虚拟主机比较多,同时有一个网友有联系到推荐入门的个人网站主机,最后建议他选择糖果主机的迷你主机方案,适合单个站点的。这次商家又推出所谓的秋季活动促销,这里一并整理看看这个服务商在秋季活动中有哪些值得选择的主机方案,比如虚拟主机最低可以享受六折,云服务器可以享受五折优惠。 官网地址:糖果主机秋季活动促...

Vultr VPS新增第18个数据中心 瑞典斯德哥尔摩欧洲VPS主机机房

前几天还在和做外贸业务的网友聊着有哪些欧洲机房的云服务器、VPS商家值得选择的。其中介绍他选择的还是我们熟悉的Vultr VPS服务商,拥有比较多达到17个数据中心,这不今天在登录VULTR商家的时候看到消息又新增一个新的机房。这算是第18个数据中心,也是欧洲VPS主机,地区是瑞典斯德哥尔摩。如果我们有需要欧洲机房的朋友现在就可以看到开通的机房中有可以选择瑞典机房。目前欧洲已经有五个机房可以选择,...

DMIT$10.9/月,日本VPS/三网直连/1核1.5G内存/20GB存储/1Gbps端口

优惠码年付一次性5折优惠码:TYO-Lite-Open-Beta-1y-50OFF永久8折优惠码:TYO-Lite-Open-Beta-Recur-20OFF日本vpsCPU内存SSD流量带宽价格购买1核1.5G20 GB4 TB1Gbps$10.9/月购买2核2 G40 GB6 TB1Gbps$16.9/月购买2核4 G60 GB8 TB1Gbps$21.9/月购买4核4 G80 GB12 TB...

linux设置环境变量为你推荐
软银巨亏为什么马云说支付宝如果被抓 知乎电脑桌面背景图片如何在电脑桌面放两张背景图片?可爱桌面背景图片浪漫桌面壁纸天玑1000plus和骁龙865哪个好各位觉得联发科最新芯片天机1000靠谱吗?真的能打过麒麟990?天玑1000plus和骁龙865哪个好哪种5G手机是联发科天玑1000芯片?燃气热水器和电热水器哪个好燃气热水器和电热水器哪个划算莫代尔和纯棉哪个好内裤是莫代尔的好还是棉质的好?理由是什么宝来和朗逸哪个好新宝来和新朗逸选哪个?好纠结!!oppo和vivo哪个好Vivo和OPPO哪个好点啊?行车记录仪哪个好行车记录仪什么牌子好
国外vps租用 域名服务器是什么 Vultr bluehost wavecom 流媒体服务器 evssl 青果网 域名转接 太原网通测速平台 免费dns解析 如何安装服务器系统 google台湾 石家庄服务器托管 德隆中文网 防cc攻击 空间申请 97rb 网络速度 架设代理服务器 更多