Ubuntu environment variable setting [posting]

Source: Internet
Author: User

In ubuntu, you can set environment variables in the following places

1
,/Etc/profile:
At login,
The first file used when the operating system customizes the user environment,
This file sets environment information for each user of the system,
When a user logs on for the first time,
The file is executed.
2
,/Etc/environment:
The second file used by the operating system at login,
The system is reading your own profile
Before,
Set the environment variable of the Environment File.
3
,~ /. Bash_profile:
The third file used for logon is. profile.
File,
Each user can use this file to input a dedicated shell.
Information,
When a user logs on,
This file is only executed once!
By default,
It sets some environment variables,
Execute the user's. bashrc
File. /Etc/bashrc:
Run bash shell for each
The user executes this file.
When BASH Shell
When opened,
The file is read.
4
,~ /. Bashrc:
This file contains your BASH Shell
Bash
Information,
When logging on and opening a new shell
,
This file is read.

Priority of several Environment Variables

1> 2> 3

 

Set permanent Environment Variables

1.
In environment variable configuration, you must first Delete. bash_profile
The three rows in about. bashrc
And then configure the environment variables in. bashrc
Medium
2.
Select the Java
Environment: Update-alternatives-config Java
3.
To make the modified environment variable take effect: source. bashrc
4.
View environment variables: ENV

It can be stored in/etc/bash/bashrc.
This is the system-level

Environment Variable commands

1
Introduction

In Linux
In the system, if you download and install the application, it is very likely that "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 the environment variable path
But the path
Is also set in Linux
Environment variables. This article is based on RedHat 9.0
, Explained in detail the problem of environment variable customization.

2
, Variable Introduction

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
Custom Environment Variables

Environment variables are
Closely related, the user starts a shell after logging on to the system.
. For Linux
Bash is generally used.
But you can also reset or switch to another shell.
. Based on the release version, bash
There are two basic system-level configuration files:/etc/bashrc
And/etc/profile
. These configuration files contain two groups of different variables: Shell
Variables and environment variables. The former is only in a specific Shell
Fixed in (such as Bash
), The latter is in different shell
. Obviously, Shell
Variables are local, while environment variables are global. The environment variable is through shell
The environment variables can be used by all programs running by the current user. For Bash
This shell
Program, you can access the corresponding environment variables through the variable name, through the Export
To set environment variables. The following describes several instances.

3.1
Use echo
Show Environment Variables

#
This example uses echo
Display common variables home

$ Echo $ home

/Home/Xiaosu

3.2
Set a new environment variable

$ Export Hello = "hello !"

$ Echo $ hello

Hello!

3.3
Use env
Command to display all environment variables

$ ENV

Ssh_agent_pid = 1875

Hostname = Xiaosu-latop

Shell =/bin/bash

Term = xterm

History Size = 1000

......

3.4
Use set
Command to display all locally defined Shells
Variable

$ Set

Bash =/bin/bash

......

3.5
Use unset
Command to clear Environment Variables

$ Export test = "test "#
Add an environment variable test

$ ENV | grep test #
This command has output to prove the environment variable test
Already exists

Test = test

$ Unset $ test #
Delete environment variable test

$ ENV | grep test #
This command has no output to prove the environment variable test
Already exists

3.6
Use readonly
Command to set read-only variables

If readonly is used
The variable cannot be modified or cleared. Example:

$ Export test = "test ..." #
Add an environment variable test

$ Readonly test #
Set the environment variable test
Set as read-only

$ Unset test #
The variable cannot be deleted.

-Bash: unset: Test: cannot unset: readonly variable
$ Test = "new "#
The variable cannot be modified.

-Bash: Test: readonly variable

3.7
Use C
Program to access and set Environment Variables

For c
Program users can use the following three functions to set or access an environment variable.

Getenv ()
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 ()
Set the function of an environment variable in the program.

Unsetenv ()
A function that clears a specific environment variable.

In addition, there is a pointer variable environ
It 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 variables by modifying 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 #
To the root directory of the user

$ LS-#
View all files, including hidden files

$ VI. bash_profile #
Modify the environment variable definition file

Then edit your path
Statement 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

It should be noted that it is best not to set the current path "./"
Put it in Path
This may cause unexpected attacks. You can use $ echo $ path
View the current search path. In this way, you can avoid frequent startup in Shell
The program beyond the search path.

 

 

Bytes -------------------------------------------------------------------------------------------------------

Original post address: http://blog.csdn.net/xiaosu_521/archive/2008/03/09/2160458.aspx

========================================================== ==============================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.