Linux environment variable settings summary

  
 

Under 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 go to the installation target folder every time, it is too cumbersome to find the executable file to operate. This involves setting the environment variable PATH, and the PATH setting is also an integral part of customizing environment variables under linux. This article is based on RedHat 9.0, which explains in detail the problem of environment variable customization.

2, Variable Introduction

Linux is a multi-user operating system
. Each user logs into the system and has a dedicated runtime environment. Usually the default environment for each user is the same. This default environment is actually a definition of a set of environment variables. Users can customize their own operating environment by modifying the corresponding system environment variables.

3, common environment variables

$PATH: determines which directories the shell will look for commands or programs

$HOME: current user home directory

$MAIL: refers to the current user's mail storage directory.

$SHELL: refers to which shell the current user is using.

$HISTSIZE: refers to the number of records saved in the history command

$LOGNAME: refers to the current user's login name.

$HOSTNAME: refers to the name of the host. Many applications usually use the hostname if they want to use the hostname.

$LANG/LANGUGE: is a language-dependent environment variable that can be modified by users in multiple languages.

$PS1: is the basic prompt, # for root users, $ for normal users, and some more complex values.

$PS2: is a sub-prompt, the default is “>”. You can modify the current command character by modifying this environment variable. For example, the following command will change the prompt to the string <;Hello, My NewPrompt :) ”.

# PS1=" Hello, My NewPrompt :) "

$IFS: Enter the domain separator. A set of characters used to separate words when the shell reads input, usually spaces, tabs, and newlines.

$0: The name of the shell script.

For example, on my Linux system:

$ echo $0

/bin/bash

$#: Number of arguments passed to the script .

$$: The process number of the shell script, which the script program usually uses to generate a unique temporary file, such as /tmp/tmfile_$$

For example, on my Linux system:

$ echo $$

31038 # indicates the current shell process number is 31038

4, export command

The export command will be imported as a variable of his parameters Go to the subshell and make it valid in the subshell. The export command creates its own parameters as an environment variable that can be seen by other scripts and programs called by the current program.

4.1 Experiments Exporting Variables

(1) Let's 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

Running this script will get the following output:

$ export1

#这是空间,是Because the variable foo is not available in export2, $foo is copied to null

The second meta-syntactic variable

$

4.2 Setting a new environment variable WELCOME< Br>

$ export WELCOME="Hello!"$ echo $WELCOMEHello!


5, Custom Environment Variables

Environment Variables are closely related to Shell After the user logs in to the system, a shell is launched. For Linux it is usually bash, but you can also reset or switch to another shell. Depending on the release version, 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 only fixed in a specific shell (such as bash ), which is fixed in different shells. Obviously, shell variables are local and environment variables are global. Environment variables are set by shell commands, which can be used by all programs running by the current user. For the bash shell program, you can access the corresponding environment variable by variable name, and set the environment variable through export. The following is illustrated by several examples.

5.1 Using the command echo to display environment variables

#This example uses echo to display common variables HOME

$ echo $HOME

/home/lqm< Br>

5.2 Setting a new environment variable

$ export HELLO=“Hello!”

$ echo $HELLO

Hello!

5.3 Display all environment variables with the env command

$ env

SSH_AGENT_PID=1875

HOSTNAME=lqm

SHELL=/bin/bash< Br>

TERM=xterm

HISTSIZE=1000

……

5.4 Display all locally defined shell variables using the set command

$ set

BASH=/bin/bash

……

Copyright © Windows knowledge All Rights Reserved