[Z] Summary of Linux environment variable settings

Source: Internet
Author: User
Tags echo command

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. Common Environment Variables

$ Path: determines the directories to which shell will look for commands or programs.

$ Home: Home Directory of the current user

$ Mail: the mail storage directory of the current user.

$ Shell: The shell used by the current user.

$ Histsize: the number of records that save historical commands.

$ LOGNAME: The Login Name of the current user.

$ Hostname: indicates the host name. Many applications usually obtain the host name from this environment variable if they want to use the host name.

$ Lang/Languge: it is a language-related environment variable that can be modified by users in multiple languages.

$ PS1: A Basic prompt. For root users, it is #. For common users, it is $. You can also use more complex values.

$ PS2: A subsidiary prompt. The default value is "> ". You can modify this environment variable to modify the current command line. For example, the following command will change the prompt to the string "Hello, my newprompt ".

# PS1 = "Hello, my newprompt"

$ Ifs: the input domain separator. When the shell reads the input, it is used to separate a group of characters of words, which are usually spaces, tabs, and line breaks.

$0: name of the shell script.

For example, in my Linux system:

$ Echo $0

/Bin/bash

$ #: Number of parameters passed to the script.

$: Process ID of the shell script. The script usually uses it to generate a unique temporary file, such as/tmp/tmfile _ $

For example, in my Linux system:

$ Echo $

31038 # indicates that the current shell process is 31038

4. Export command

The Export command imports the variable as its parameter to the sub-shell and makes it valid in the sub-shell. The Export command creates a parameter as an environment variable, which can be seen by other scripts and programs called by the current program.

  4.1 Export experiment Variables

(1) Let's first list the script program export2

#! /Bin/sh

Echo "$ foo"

Echo "$ bar"

(2) then the script export1. At the end of this script, we call export2:

#! /Bin/sh

Foo = "the first meta-syntactic variable"

Export bar = "the second meta-syntactic variable"

Export2

Run this script to get the following output:

$ Export1

# This is a space, because the variable foo is not available in export2, so $ foo is copied as null

The second meta-syntactic variable

$

  4.2 set a new environment variable welcome

$ Export welcome = "Hello! "
$ Echo $ welcome
Hello!

 

5. 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.

  5.1 use the echo command to display environment variables

# In this example, ECHO is used to display common variables home

$ Echo $ home

/Home/lqm

  5.2 set a new environment variable

$ Export Hello = "Hello !"

$ Echo $ hello

Hello!

5.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

......

  5.4 use the set command to display all locally defined shell Variables

$ Set

Bash =/bin/bash

......

5.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

  5.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

  5.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;

}

  5.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.

$ VI/etc/bashrc # modify shell Variables

$ VI/etc/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.

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.