Introduction to linux environment variables

  

Today introduces you to the basics of linux environment variables. If you choose to use linux; then it is inevitable to deal with these environment variables (windows also have this stuff). Each linux process will have a set of environment variables corresponding to the key/value. The program can control these environment variables or local environment variables (the environment variables in the child process will not affect the main process, this is similar to the c function call ^_^). We can interact with these environment variables in the shell process (similar to the DOS command line), of course, you can also import variables into the child process. Introduce to introduce the setting and use of environment variables.

First, set local variables

Set the environment variable for the shell is quite simple, type name=value in bash. In tcsh (this shell seems to have been out ^_^) in front of you need to add a set command set name=value

Second, set the global variable

The method described above applies to the current process. If you want variables in the child process to be used globally, you need to use the export command. Enter export name=value in bash. Setenv name=value in tcsh.

Three, set variables for the specified process

The two methods described above are quite common in practical applications. But this alone is not enough. Sometimes we want to set variables with the same name but different values ​​for different child processes. What do we need to do? The answer is simple name=value program . For example: LANG=C sort, this command sets the LANG=C environment variable for the sort program. This setting only works for the sort program. Quite humanized ^_^. In addition, you need to add an env in tcsh, env name=value sort

4. View variables

View and display variable utility echo commands in linux, such as echo $name. The $ symbol for friends who are doing php must be very familiar with it. In Linux, only the reference variable will be added. In bash we can use the set command to see all the variables. Use printenv to see all global variables. One thing to note: in tcsh the set command can only view local variables, while printenv can only display all shell built-in variables.

V. The use of variables

The use of variables is quite extensive, involving all aspects of the Linux program. With the introduction above. I believe that netizens are no strangers to the Linux variable. Let's write this today.

Copyright © Windows knowledge All Rights Reserved