1. Introduction
In Linux, If you download and install an application, it is very likely that the prompt "command not found" appears when you type its name. If you find the executable file in the installation target folder every time, it is too cumbersome to find the executable file. This involves setting the environment variable path, and the path setting is also an integral part of customized environment variables in Linux. Based on RedHat 9.0, this article describes how to customize environment variables in detail.
2. Introduction to variables
Linux is a multi-user operating system. Each user logs on to the system and has a dedicated runtime environment. Generally, the default environment of each user is the same. The default environment is actually the definition of a set of environment variables. You can customize your running environment by modifying the corresponding system environment variables.
3. Customize Environment Variables
Environment variables are closely related to shell. A shell is started after the user logs on to the system. For Linux, bash is generally used, but you can reset or switch to another shell. Based on the release version, Bash has two basic system-level configuration files:/etc/bashrc and/etc/profile. These configuration files contain two different variables: Shell variables and environment variables. The former is only fixed in a specific shell (such as Bash), and the latter is fixed in different shells. Obviously, shell variables are local, while environment variables are global. Environment variables are set through shell commands. The configured environment variables can be used by all programs run by the current user. For the bash shell program, the corresponding environment variables can be accessed through the variable name, and the environment variables can be set through export. The following describes several instances.
3.1 Use the echo command to display environment variables
# In this example, ECHO is used to display common variables home
$ Echo $ home
/Home/lqm
3.2 set a new environment variable
$ Export Hello = "hello !"
$ Echo $ hello
Hello!
3.3 Use the Env command to display all environment variables
$ ENV
Ssh_agent_pid = 1875
Hostname = lqm
Shell =/bin/bash
Term = xterm
History Size = 1000
......
3.4 use the set command to display all locally defined shell Variables
$ Set
Bash =/bin/bash
......
3.5 run the unset command to clear environment variables.
$ Export test = "test" # Add an environment variable test
$ ENV | grep test # This command has output to prove that the environment variable test already exists.
Test = test
$ Unset $ test # Delete the environment variable test
$ ENV | grep test # This command has no output, proving that the environment variable test already exists
3.6 use the readonly command to set read-only variables
If the readonly command is used, the variables cannot be modified or cleared. Example:
$ Export test = "test..." # Add an environment variable test.
$ Readonly test # Set the environment variable test as read-only
$ Unset test # This variable cannot be deleted
-Bash: unset: Test: cannot unset: readonly variable
$ Test = "new" # This variable cannot be modified
-Bash: Test: readonly variable
3.7 Use a c program to access and set Environment Variables
For C program users, you can use the following three functions to set or access an environment variable.
Getenv () is used to access an environment variable. The input parameter is the name of the variable to be accessed, and the return value is a string. If the accessed environment variable does not exist, null is returned.
Setenv () is a function used to set an environment variable in a program.
Unsetenv () is a function used to clear a specific environment variable.
In addition, the pointer variable environ points to a list containing all environment variables. The following program can print all the environment variables in the current running environment:
# Include <stdio. h>
Extern char ** environ;
Int main ()
{
Char ** var;
For (Var = environ; * var! = NULL; ++ var)
Printf ("% s/n", * var );
Return 0;
}
3.8 modify the environment variable definition file.
It should be noted that, in general, this is only applicable to common users, avoid modifying the root user's environment definition file, because it may cause potential risks.
$ CD # Go to the user root directory
$ LS-A # view all files, including hidden files
$ VI. bash_profile # modify the environment variable definition file
Edit your path declaration in the following format:
Path = $ path: <Path 1 >:< Path 2 >:< Path 3 >:------: <path n>
You can add the specified path by yourself, separated by a colon in the middle. After the environment variable is changed, it will take effect the next time you log in. If you want to take effect immediately, you can execute the following statement: $ source. bash_profile
Note that it is best not to put the current path "./" in the path, which may be subject to unexpected attacks. After that, you can view the current search path through $ echo $ path. In this way, you can avoid frequent startup of programs outside the shell search path.
4. Summary
With the above settings, you can have a more convenient and effective environment to improve your work efficiency.