1 Introduction
In linux system, if you download and install the application, is likely to type its name when "command not found" prompt content. If the target every time to the installation folder, locate the executable file to be too cumbersome to operate the. This involves setting the environment variable PATH problem, PATH is also set custom environment variables in linux under a part of. Based on RedHat 9.0, custom environment variables explained in detail the problem.
2, variable profile
Linux is a multi-user operating system. Each user login system, will have a dedicated operating environment. Usually the default environment for each user are the same, the default environment is actually the definition of a set of environment variables. Users can customize their own operating environment, which is to modify the system environment variables accordingly.
3, custom environment variables
Shell environment variables are closely related and, after the user login system launched a Shell. For Linux it is usually bash, but can also be reset or switch to other Shell. According to the release version of the case, bash has two basic system-level configuration files: / etc / bashrc and / etc / profile. These configuration files contain two different sets of variables: shell variables and environment variables. The former is fixed in a particular shell (eg bash), the latter fixed in different shell. Obviously, shell variables are local, and environmental variables are global. Environment variable is set through the Shell command to set environment variables can also be good for all current users are used to run the program. The Shell program is for the bash, you can access the corresponding variable names to environment variables, through export to set environment variables. Illustrated by several examples below.
3.1 Display the environment variable using the command echo
-------------------------------------------------- ------------------------------
# This example uses the echo display common variable HOME
$ Echo $ HOME
/ Home / lqm
-------------------------------------------------- ------------------------------
3.2 sets a new environment variable
-------------------------------------------------- ------------------------------
$ Export HELLO = "Hello!"
$ Echo $ HELLO
Hello!
-------------------------------------------------- ------------------------------
3.3 use the env command to display all the environment variables
-------------------------------------------------- ------------------------------
$ Env
SSH_AGENT_PID = 1875
HOSTNAME = lqm
SHELL = / bin / bash
TERM = xterm
HISTSIZE = 1000
... ...
-------------------------------------------------- ------------------------------
3.4 use the set command to display all local variables defined in Shell
-------------------------------------------------- ------------------------------
$ Set
BASH = / bin / bash
... ...
-------------------------------------------------- ------------------------------
3.5 use the unset command to clear the environment variables
-------------------------------------------------- ------------------------------
$ Export TEST = "test" # add an environment variable TEST
$ Env | grep TEST # This command has output that has been in existence for the environment variable TEST
TEST = test
$ Unset $ TEST # delete the environment variable TEST
$ Env | grep TEST # this command no output, that the environment variable TEST already exists
3.6 use the readonly command to set read-only variables
If you use the readonly command, then the variable can not be modified or eliminated. Examples are as follows:
-------------------------------------------------- ------------------------------
$ Export TEST = "Test ..."# add an environment variable TEST
$ Readonly TEST # TEST environment variable is set to read-only
$ Unset TEST # will find that this variable can not be deleted
-Bash: unset: TEST: cannot unset: readonly variable
$ TEST = "New" # will find that this variable can not be modified
-Bash: TEST: readonly variable
-------------------------------------------------- ------------------------------
3.7 with a C program to access and set the environment variable
C program for users who can use the following three functions to set or access an environment variable.
getenv () to access an environment variable. Input parameter is the need to access the variable name, the return value is a string. If the access to environment variables does not exist, it will return NULL.
setenv () in the procedures set an environment variable inside a function.
unsetenv () environment variables remove a specific function.
There is also a pointer variable environ, which point to include all of the environment is a list of variables. The following program can print out the current operating environment, which all environment variables:
-------------------------------------------------- ------------------------------
# Include
extern char ** environ;
int main ()
(
char ** var;
for (var = environ; * var! = NULL; + + var)
printf ("% s
", * Var);
return 0;
)
-------------------------------------------------- ------------------------------
3.8 by modifying the environment variable definition file to modify environment variables.
Note that the yes, generally Xia, this is only applicable for ordinary users, avoid modifying the root user's Huanjingdingyi Wen Jian, because that may cause potential dangers.
-------------------------------------------------- ------------------------------
$ Cd # to the user in the root directory
$ Ls-a # View all files, including hidden files
$ Vi. Bash_profile # modify the environment variable definition file
-------------------------------------------------- ------------------------------
Then edit your PATH statement, the format is:
PATH = $ PATH ::::------:
You can own a prescribed path, the middle separated by a colon. Environment variable changes, the user takes effect the next landing, if you want to immediately take effect, you can execute the following statement: $ source. Bash_profile
Note that, it is best not to the current path. "/" Into the PATH, the way the attack may be unexpected. Upon completion, you can view the current $ echo $ PATH search path. After this customization, you can avoid frequent start-up in the shell search path outside the proceedings.
4 Summary
The above settings, you can have a more convenient and effective environment to improve the efficiency of your work.