linux bash environment variables simple summary

Source Link:

Original article, if reproduced, please indicate the original address

I. Introduction Environment Variables

Linux is a multiuser operating system. After each user login system, you will have a dedicated operating environment. Typically, each user's default environment

Is the same, the default environment is actually the definition of a set of environment variables.

Environment variables are global, set a good environment variables can be used by all current users of the program running.

Users can customize their own operating environment, which is to modify the system environment variables.

Common environment variables:

PATH: the shell to determine which directories to find commands or programs

ROOTPATH: the same function and the PATH variable, but this one only lists the directories that should be checked when the superuser (root) type the command.

HOME: The current user's home directory

USER: View the current user

LOGNAME: View the current user's login name.

UID: The current user identification word, the value is a string consisting of digits.

SHELL: refers to the current user of what type of Shell.

TERM: types of terminals.

PWD absolute pathname of the current working directory, the value of the variable changes with using the cd command.

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

HISTSIZE: it refers to the number of saved history commands record

HOSTNAME: refers to the name of the host, if many applications to use a host name, it is usually from this environment variable to get in.

PS1: basic prompt, be # for the root user, for the average user is $, you can also use some of the more complex values.

PS2: is a subsidiary of the prompt, the default is ">." Can be modified by modifying the current command operator environment variable, for example the following command will prompt

Modified string "Hello, My NewPrompt :)". # PS1 = "Hello, My NewPrompt :)"

IFS: input field separator. When reading input shell for partitioning a set of characters of the word, they are usually spaces, tabs, and line breaks.

Language and character set environment variables important environment related to:

Related command:

stty character set or display terminal settings, main keys representational

locale (local environment) view the current language information

[Note: locale -a: View the current character set supported by the system]

The default value when LANG variable is not set any LC_XXX used.

[LANG = zh.CN.UTF-8 gedit a.txt // temporary use other language character sets specified application open]

LC_ALL is used to overwrite all the values ​​of other variables LC_XXX

LC_COLLATE collation of the Region

LC_CTYPE character set specified area (ie: letters, numbers, punctuation, etc.)

LC_MESSAGES response information using designated areas; [Note: applies only POSIX]

LC_MONETARY currency format specified regions

LC_NUMERIC use of digital format Region

LC_TIME date and time using the format specified areas.

Less common practical environment variables:

LD_LIBRARY_PATH This variable contains a colon-separated list of directories, dynamic link library files will find in these directories.
MANPATH This variable contains a colon-separated list of directories, the man command searches for the man pages directory.
[NOTE: man help directory structure must be: man / {man1, man2, .. man8}, MANPATH = / man ]
INFODIR This variable contains a colon-separated list of directories to search for these command info directory info page.
PAGER This variable contains the path to browse the contents of the program file (such as less or more).
EDITOR This variable contains the path to change the contents of the program (file editor) (such as nano or vi).

System location variables - [only for command-line shell script to get the parameters passed to the script]

$ 0, $ 1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7 $ 8 $ 9, $ * -> $ 0 to 9, and the windows of the same meaning as 0-9.

$ 0: Shell currently executing the file name

$ #: The number of positional parameters in the command line

$ *: Get all the current parameters of the shell

$ @: All the parameters of this program

System special variable

A command execution status return $ ?: post, when the return value 0: table performs a normal, non-zero: error or abnormality

Program execution, there may be two types of return values:

"0: correct execution

"1 to 255: Incorrect execution. Wherein 1,2,127 is reserved by the system error code, may be used on their own

$$: the current process where the process [The most common use $$ variable is used as the name of the temporary file to ensure that the temporary files will not be repeated. ]

$ !: running in the background of the last process ID

$ _: Last parameter execute a command or script before this

Quotes impact on the variable assignment:

"Double quotation marks" ": allows you to reference other variables value by $ symbol

'Single quotation marks'': Do not reference other variables value $ treated as ordinary characters

"Anti-apostrophe` `: the results of the command to change the output variable,

$ (...): This is another version of the anti-apostrophe.

Two .Bash (Bourne-Again Shell) Introduction

Shell Bash is the default in many Linux platforms, in fact, as well as with the many traditional UNIX Shell, like tcsh, csh, ash, bsh, ksh

and many more. Shell Script are roughly similar, when you learn a Shell after the other Shell will soon get started.

Most of the time, usually a Shell Script can be used on a wide variety of Shell.

In general, the shell function is acquired from the terminal or other input command, parses it into series of operating instructions, or the corresponding system call kernel

External program execution, and the execution result is returned to the terminal or other output. Therefore, to achieve a simple shell is an easy task.

But not limited to bash functionality, it supports collaboration with pipes and redirection command execution, provides a powerful scripting ability,

With job management capabilities. General Linux distributions, bash executable files is often one / bin some of the largest utilities,

Objectively reflects its complexity.

About three .Bash implementation principle

bash process using GNU Readline library user command input, Readline provides vi or emacs like line editing features.

Bash dispatch center which is run cycle master. Cycle master function is relatively simple, it is read cyclically user (or script) input is transmitted to

Parser, while the underlying recursive processing error is returned.

Input to the text parser first wildcard, aliases, and variable expansion arithmetic, etc., and obtained by the command generator

Standard command structure by a special mechanism to fill the redirection process to redirect semantics, handed over command executes. Command executor basis

Different types of command, internal execution command function, an external program or file system calls. During execution of the command, the system signal to the actuator

Capture and processing. In the operating system to support operations management, the command will process information added to job control mechanism, and allows users to

Using the keyboard or the internal command signal to start and stop operations. If you compile bash, use another set of interfaces in the operating system does not support the job management

The same mechanism to process information for simple maintenance.

Four - .Shell and environment variables

And Shell environment variables are closely related, environment variables are set by the Shell command. After user login system launched a Shell.

For Linux generally bash, but you may also be reset or switch to other Shell.

Set a good environment variables and can be used by all current users of the program running. The Shell program is for the bash, you can

Variable name to access the appropriate environment variable.

Linux environment variables are inherited, that child will inherit the parent shell shell environment variables.

The current shell variables, also called a local variable, it is clear that local variables definitely include environment variables. Non-local variables Linux environment variables

Do not have inheritance.

Fifth, the order of execution of the login script: [Note: applies only to bash shell]

Login Shell

When refers login, you need to provide shell username and password, such as: su - user1, graphical login, ctrl + alt + F2-6 to enter login interface.

This Login shell script execution order:

1. / etc / profile file [global profile; it defines LoginUser of PATH, USER, LOGNAME (login user account)]

Scripts directory under 2. /etc/profile.d/

3. ~ / .bash_profile [path search command level ------- login environment variable configuration file]

4. ~ / .bashrc [Storage and functions set by the user alias variable level of ambient ---- the shell profile]

5. / etc / bashrc [global bashrc file; it mainly define Function and Alias; to change the / etc / bashrc affects all users, managed by the root user. ]

Non-Login shell

Non-login shell means that do not need to enter a user name and password of the shell, such as the right graphics terminal, or ctrl + shift + T to open the shell

This Non-Login shell execution order logon script:

1. ~/.bashrc

2. /etc/bashrc

Scripts directory under 3. /etc/profile.d/

When it comes to user scripts can not say, the two documents are closely related to the user:

1. /etc/login.defs - defines the user password length, maximum use period, the number of days warning, password encryption, the default home directory permissions.

2. / etc / default / useradd - which defines when creating a user, the default home directory of the root, the default shell, where to copy the necessary configuration files, such as whether to create a mailbox.

/ Etc / skel - [. Bashrc, .bash_profile, .bash_logout other documents all from here]

When you add a user command, it will default to a new directory called user name in the / home / under, and the files in the / etc / skel

copy to the new user's home directory. So that when the user logs in, the system will automatically call these files, configure the user's login environment.

Six applications and operating environment variables:

1. remove and replace the contents of variables

$ {# Variable} keyword from scratch check keywords, matching the shortest data will delete

$ {Keyword} variable ## from the beginning to check keywords, will be in line with the longest data deletion

% $ {Variable} keyword, the shortest data conforming to start checking the tail removed from Image

%% $ {variable} keyword, the longest matching data deleted from the beginning to check the tail Image

$ {Variable / old string / new string} if they meet the old string, the first string is replaced old

$ {String variable // old / new string} if they meet the old string, the string is replaced all old

2. Replace and set the variable content

A variable displacement:

Variable has no value:

echo $ {a: = abc} // If a NO value, to give a one abc, as its value, then the value of a permanently abc is, until the next modification.

// The following example is not used

echo $ {a :? necessarily need a value to a variable} // if not a value, the prompt "needs to be a value to a variable"

echo $ {a: -abc} // if not a value, a value of a lent, once the command has completed, or a null value.

Variable has a value

echo $ {a: +123} // value of the temporary variable replacement; a = 1; expr $ {a: +123} - 1; a value of 122

Secondary variable displacement

Example 1:

A=B

a=1

B1 = "B value"

eval echo \ $$ A $ a> B1.txt // Note: This can not get a direct result of the secondary replaced with anti-apostrophe.

C = `cat B1.txt` // thus can obtain the value of the variable B1.

Example Two:

FAIL="echo -e \"\n\e[31m\${FMSG}\e[0m\n\""

FMSG = "prompt string red display needs." && eval $ {FAIL}

Example Three:

grep results: "APPDIR =" / home / abc / bin "" APPDIR can be directly converted to an assignment statement is executed by eval.

eval `grep '^ .*APPDIR' /etc/tuxconfig`

echo $ {APPDIR} // This will output the value APPDIR.

VII. Some Shell built-in command

1.source command

Role: reading and FileName in the command is run in the bash environment, the environment variables to take effect.

Note: This command is normally used instead of command. "."

E.g:

source ~/.bash_profile

2.export :

For the variable becomes the current shell environment variables and their sub-shell, and survival is the current shell and its sub-shell, thus re-visit or close the current

After the shell and its sub-shell, it sets the environment variable it disappears.

3.env和printenv

These two variables are used to print all the environmental variables:

4.set

For displaying the current setting local variable. A single set to show all the current environment variables, which certainly include environmental variables and some non-environmental variables

5.unset
used to clear the variables. Whether this variable is an environment variable or a local variable, it can be removed.

VIII. Other variables associated with little knowledge

shell: are weakly typed languages, variables can be used directly, you do not need to be declared and initialized.

For a strongly typed language, you must declare variables before using, or even need to initialize, because the variable whose value does not initialize a random number.

Variables: It is an alias memory space, the alias mapping is addressed memory cell memory.

Array: is named a contiguous memory space

Shell types of variables: the storage format is determined in advance and the data length.

Characters: the ASCII is stored; 10 a string, the ASCII code is stored as two, accounting for 16bit.

Value:

Integer 1: 10, such as when it is stored, is converted to binary 1010, accounting for 4bit, the actual accounting 8bit.

2. Float: "11.23", which at the time of storage, will be stored into two parts,

Decimal position values ​​and generally, as 11.23-> 0.1123 * 10 ^ 2, when the remaining storage number 1123 and a power of two.

As: 2014/11/11 in the storage, if it is stored as strings 64bit.

The stored value is used by 2014/11/11 - 1977/1/1 = N, if N is the value which memory space required up to 24bit.

Variable overflow:

Is an integer 8bit, values ​​can be stored in the 256, range 0-255, but if an integer is stored in the 256, the most how? The result is a variable overflow,

Is stored in the whole 8bit 0,1 overflow, the overflow after being stored in a storage space in the other programs, it may overwrite the data of another program.

It did not seem to make any sense, but if it is a well-designed overflow value, it may therefore overwrite part of the memory space for some programs

Thus easily gain control over the program, if the program is a system process, it will be possible to obtain system privileges

shell in an array of related operations: - [Note: an array of the shell can not be used as an object export export, so it can not only as an environment variable]

Variable name = (Value1 Value2 Value3 Value4)

a=(1 2 3 4 5)

example:

People (s): 1.0% us, 0.5% sy, 0.0% Ni, 98.5% id, when 0.0%, 0.0% hi, 0.0% si, 0.0% st

a=(`top -n5 -d1 | awk '$1~/Cpu\(s\):/{print $2}' | awk -F'%' '{print $1}' | xargs -n5`)

Check the number of array members

echo $ {# a [*]} -> preceded by "#" represents the array to see how many members

View member value

echo $ {a [0]} -> value of the first array member 1.

echo $ {a [*]} or echo $ {a [@]} -> Check value of the member so

declare -pa -> View all array members.

echo $ {a [*]: 1: 3} -> Check the array index value is 1 to 3 members

Guess you like

Origin www.cnblogs.com/woshijiuke/p/11505430.html