When we install the system software under Linux, we often encounter some problems in the configuration of the system environment variables. What is an environment variable? How do I customize environment variables? I'll do some introductions below.
First, what is the environment variable?
Linux is a multi-user operating system. Multi-user means that each user logs into the system and has their own dedicated operating environment. This environment is defined by a set of variables, which are called environment variables. Users can modify their environment variables to meet the requirements of the environment.
Second, custom environment variables
Environment variables are closely related to the shell and are set by shell commands. Environment variables can also be used by all programs that are running by the current user. For bash, the variable name can be used to access the corresponding environment variable.
Here are a few examples to illustrate
1. Display Environment Variables Home
$ echo $HOME
/home/admin
2. Set a new environment variable name
$ Export Name="Raidcheng"
$ echo $NAME
Raidcheng
3. Use the ENV command to display all environment variables
$ env
Hostname=test
term=vt100
Shell=/bin/bash
histsize=1000
Ssh_client=202.xxx.xxx.xxx 53694 22
Catalina_base=/usr/local/jakarta-tomcat
ssh_tty=/dev/pts/0
Ant_home=/usr/local/ant
Java_opts=-server
User=admin
...
4. Use the SET command to display all locally defined shell variables
$ set
Bash=/bin/bash
bash_versinfo= ([0]="2"[1]="05b"[2]="0"[3]="1"[4]="Release"[5]="I386-redhat-linux-gnu")
bash_version='2.05b.0 (1)-release'
Catalina_base=/usr/local/jakarta-tomcat
Catalina_home=/usr/local/jakarta-tomcat
...
5. Use the unset command to clear environment variables
$ Export Name="Raidcheng"
$ echo $NAME
Raidcheng
$ unset NAME
$ echo $NAME
6. Set read-only variables using the readonly command
$ Export Name="Raidcheng"
$ readonly NAME
$ unset NAME
-bash:unset:name:cannot unset:readonly Variable
$ name="New"#会发现此也变量不能被修改
-bash:test:readonly variable
Three, the common environment variable
PATH determines to which directories the shell will look for commands or programs
Home Current User Home directory
Histsize Number of historical records
LOGNAME The current user's login name
HOSTNAME refers to the name of the host
Shell Front user shell type
Languge language-related environment variables, multiple languages can modify this environment variable
Mail storage directory for the current user of mail
PS1 basic prompt, for root user Yes #, for normal user is $
PS2 attached prompt, default is ">"
Iv. Accessing and setting environment variables through C programs
For users of C programs, you can use the following three functions to set or access an environment variable.
Getenv () accesses an environment variable. The input parameter is the name of the variable that needs to be accessed, and the return value is a string. If the environment variable you are accessing does not exist, it will return null
Setenv () a function that sets an environment variable inside a program
Unsetenv () function to clear a specific environment variable
In addition, there is a pointer variable environ, which points to a list that contains all the environment variables. The following program can print out all environment variables in the current operating environment:
#include
extern Char**environ;
int main ()
{
Char**var;
for (var =environ;*var!=null;++var)
printf ("%s \ n", *var);
return 0;
}
V. Environment variables File
Modify the environment variables by modifying some of the relevant environment definition files, for example, for Redhat, the environment-related files are/etc/profile and ~/.bash_profile, etc. Once the modification is complete, log back in again or run the command source XXX is in effect.
Many of the people who do Java development under Linux will encounter
JDK
settings for Environment variables, this article is necessary for beginners. Installation of JDK under Linux and settings for Java environment variables
1 , to www.sun.com download the latest version of J2SDK for Linux
2 , installing jdk1.4.2 for Linux, the following is an example under Redhat Linux 9:
Log in as Root
(1) Open a terminal
(2) Enter command Ls-l to view the installation file permissions
(3) Enter the command chmod a+x j2sdk-1_4_2_01-linux-i586-rpm.bin to add the Execute permissions to all users.
(4) Installing the JDK
./j2sdk-1_4_2_01-linux-i586-rpm.bin
When this step is complete, the j2sdk-1_4_2_01-linux-i586.rpm file is generated
View Permissions
Ls-l
Assigning Execute permissions to j2sdk-1_4_2_01-linux-i586.rpm
chmod +x j2sdk-1_4_2_01-linux-i586.rpm
Installing j2sdk-1_4_2_01-linux-i586.rpm
RPM-IVH j2sdk-1_4_2_01-linux-i586-rpm
The authorization agreement appears, press ENTER to accept
(5) Use the default installation location, under/usr/java/, i.e./usr/java/j2sdk1.4.2_01
3 , Set environment variables
Edit the environment variables using the VI editor:
Entering commands in the terminal
vi/etc/profile.d/java.sh
This can be used in all users
In the new java.sh, enter the following:
#set Java Environment
Export j***a_home=/usr/java/j2sdk1.4.2_01
Export classpath=.: $J ***a_home/lib: $J ***a_home/jre/lib
Export path= $J ***a_home/bin: $J ***a_home/jre/bin: $PATH
Save exit
Both path and CLASSPATH specify a list of paths, separated by delimiters between the items in the list (that is, the individual paths). Under Windows, the delimiter is a semicolon (;), and under Linux, the delimiter is a colon (:).
4 , assigning permissions to java.sh
Enter the following command:
chmod 755/etc/profile.d/java.sh
5 , Restart your computer
Test if the JDK is successfully installed by entering the following command in the terminal
Java-version
If you see information about the JVM, the installation succeeds
Installation of JDK under Linux and Settings for JDK environment variables