Linux environment variables

Source: Internet
Author: User
Tags echo command

This article address:http://www.cnblogs.com/archimedes/p/linux-envionment-variables.html, reprint please indicate source address.

1. What are environment variables

Bash Shell uses a feature called environment variables to store information about shell sessions and the work environment, which allows you to store data in memory so that programs and scripts running on the shell can access

In the bash shell, environment variables fall into two categories: global variables and local variables

(1) Global environment variables

Global environment variables are not only visible to shell sessions, but are useful to all shell-created processes that need to obtain information about the parent process. Linux systems set some global environment variables before you start a bash session

System environment variables are used in all uppercase letters to distinguish the environment variables from ordinary users

To view global variables, you can use the printenv command (due to too many results, do not write them)

to display a single environment variable, you can use the echo command . When referencing an environment variable, you must place a $ character before the name of the environment variable:

[Email protected]:~/ccode$ Echo $HOME
/home/wu

(2) Local environment variables

Local environment variables can only be seen in the process in which they are defined, and the list of local environment variables is a bit complex, and there is no such command in a Linux system to show only local environment variables

The SET command displays all environment variables that are set by a particular process, including, of course, global variables

2. Setting environment variables (1) Setting Local environment variables

Once you launch the bash shell, you can create local variables that are visible within the shell process, and you can assign values to environment variables by using the equals sign, which can be numeric or string:

[Email protected]:~$ test=testing
[Email protected]:~$ Echo $test
Testing

If you want to assign a string containing spaces to a variable, you must use single quotation marks to define the start and end of the string:

[Email protected]:~$ test=testing a long string
A:command not found
[Email protected]:~$ test= ' testing a long string '
[Email protected]:~$ Echo $test
Testing a long string

(2) Setting Global environment variables

The way to create a global environment variable is to create a local environment variable before exporting it to the global environment

This process is done through the Export command:

[Email protected]:~$ Echo $test
Testing a long string
[Email protected]:~$ Export Test
[Email protected]:~$ Bash
[Email protected]:~$ Echo $test
Testing a long string

(3) Deleting environment variables

You can use the unset command to delete an environment variable:

[Email protected]:~$ Echo $test
Testing a long string
[Email protected]:~$ unset test
[Email protected]:~$ Echo $test

[Email protected]:~$

3. Default shell environment variables

1. BASH: Record the path of the current BASH shell.

2. Bash_subshell: Records the level of the current child shell. Bash_subshell is an integer that counts from 0 onwards.

3, Bash_versinfo: is an array containing six elements, and these six elements show the version information of BASH.

4. Bash_version: Displays the shell version information.

5, Dirstack: Record the directory value of the top of the stack, empty value.

6. Globlgnore: is a list of patterns separated by colons, representing a collection of filenames that are ignored when wildcard.

7. GROUPS: Records the group to which the current user belongs.

8. Home: Records the current user's home directory, which is determined by the penultimate domain of/etc/passwd.

9, HOSTNAME: Record the hostname.

10, HostType and Machtype: Are the hardware architecture of the recording system.

11. IFS: Used to set the specified shell domain delimiter, which by default is a space.

12, Oldpwd: Record the old working directory.

13, OSTYPE: Record the operating system type.

14. Path: Environment variable that displays the contents of the current PATH environment variable.

15, PPID: Is the process number that creates the current process, that is, the parent process number of the current process

16. PS1: Prompt variable to set the prompt format for setting the first level shell prompt environment variable.

17, PS2: Used to set the level two shell prompt environment variable.

18. PWD: Record the current path

19. The reply:reply variable is related to read and select.

20, SECONDS: Record the time that the script takes from start to finish.

21. Shell: Displays the currently used shell

22. Shellopts: A list of shell options in the "On" state is recorded, it is just a read-only variable.

23. SHLVL: The level of bash nesting is recorded, as a general rule, when we start the first shell. $SHLVL = 1. If the script is executed in this shell, the $shlvl=2 in the script.

24, Tmout: Used to set the time when the script expires, such as tmout=3, indicating that the script expires after 3 seconds.

25. UID: The ID of the logged-on user

26, User: Display the current user name

4. Set the PATH environment variable

Path defines the search path for the command line Input command, which produces an error if the command is not found:

[Email protected]:~$ Myprog
Myprog:command not found

You can add a new search directory to an existing PATH environment variable without having to define it from scratch, and the directories in path are separated by colons, so you simply reference the original path value and then add a new directory to the string.

5. Variable array

Environment variables a good function is that they can be used as an array, to set a number of values for an environment variable, you can put the value in parentheses, the value and value are separated by a space:

[Email protected]:~$ mytest= (one, three four five)
[Email protected]:~$ Echo $mytest
One

Only the first value is displayed. To reference a single array element, you must use a numeric index value that represents its position in the array, enclosed in square brackets:

[Email protected]:~$ echo ${mytest[2]}
Three

To display the entire array variable, use the asterisk as a wildcard to place the index value:

[Email protected]:~$ echo ${mytest[*]}
One three four five

You can also change the value of an index position:

[Email protected]:~$ Mytest[2]=seven
[Email protected]:~$ echo ${mytest[*]}
One seven four Five

You can even use the unset command to delete a value in an array, but be careful:

[Email protected]:~$ unset mytest[2]
[Email protected]:~$ echo ${mytest[*]}
One and four five
[Email protected]:~$ echo ${mytest[2]}

[Email protected]:~$ echo ${mytest[3]}
Four

Finally, you can follow the unset command followed by the array name to delete the entire array:

[Email protected]:~$ unset mytest
[Email protected]:~$ echo ${mytest[*]}

[Email protected]:~$

Linux environment variables

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.