Environment variable
In Bash and almost all other shells, users can define environment variables that store them in ASCII string. The most convenient environmental variable is that they are the standard part of the UNIX process model. This means that environment variables are not only used by shell scripts, but also by compiled standard programs. When "Export" environment variables in Bash, any program running later, no matter whether it is a shell script, you can read the settings. A good example is the vipw command, which usually allows the root user to edit the system password file. By setting the Editor environment variable into a favorite text editor name, you can configure the VIPW to make it use the editor, without using VI, if you are accustomed to Xemacs, you don't like VI, then this is convenient.
The standard method for defining environment variables in Bash is:
$ myvar = 'this is my environment variable!'
The above command defines a environment variable named "MyVar" and contains strings "this is my environment variable!". There are some precautions above: First, there is no space on both sides of the equal sign "=", and any space will result in an error (try to see). The second part should pay attention to: Although the quotation marks can be omitted when defining a word, the quotation marks are required when the defined environment variable value is more than one word (including spaces or tabs).
Third, although it is usually used to replace single quotes, in the above example, this will result in an error. why? Because single quotes are disabled, the Bash feature called extended is disabled, where special characters and character series are replaced by a value. For example, "!" Characters are historical extensions characters, BASH usually replaces them with commands previously entered. (Historical extensions will not be described in this series, as it is not commonly used in Bash programming. For more information on historical extensions, see the "Historical Extensions" section in the Bash Help page.) Although this is similar to the macro Convenient, but now we only want to add a simple exclamation point behind the environment variable, not a macro.
Now let's take a look at how actually use environment variables. This has an example:
$ Echo $ MyVar
This is my environment variable!
Based by adding one $ in front of the environment variable, Bash can replace it with myvar's value. This is called "variable extension" in Bash terms. But what will this be:
$ echo foo $ myvarbar
foo
We hope to look back "Foothis is my environment variable! Bar", but not this. Where is wrong? Simply put, Bash variable extension facilities have been confused. It can't identify which variable to expand: $ m, $ my, $ myvar, $ myvarbar, etc. How do I clearly tell which variables? Try this:
$ echo foo $ {myvar} bar
Foothis is my environment variable! bar
As you can see, when the environment variable is not clearly separated from the surrounding text, it can be enclosed with the curneth. Although $ MyVar can be entered faster, it works correctly in most cases, but $ {myvar} can be correctly analyzed in almost all situations. In addition, the two are the same as the remainder of this series will be seen in the remainder of this series. Keep in mind: When the environment variable does not use blank (spaces or tabtles) to separate the surrounding text, please use a more clear curly parentheses. Recall that we also mentioned that "export" variables can be "exported. When the environment variable is exported, it can automatically use any scripts or executable program environments running later. The shell script can use the shell's built-in environment variable to support the "arrival" environment variable, and the C program can be called using the GetENV () function. Here are some C code examples, input and compile them - it will help us understand environment variables from the angle: MyVar.c - Sample Environment Variable C Program
#include
#include
INT main (void) {
Char * myenvvar = getenv ("editor");
Printf ("The Editor Environment Variable IS Set TO% S / N", MyEnvvar;
}
Save the above code to file myenv.c, then issue the following command to compile:
$ gcc myenv.c -o myenv
Now, there will be an executable program that will print the value of the Editor environment variable at runtime (if worthless). This is when running on my machine:
$ ./myenv
The Editor Environment Variable IS Set To (NULL)
Ah ... Because the Editor environment variable is not set to any value, the C program gets an empty string. Let's try to set it to a specific value:
$ Editor = Xemacs
$ ./myenv
The Editor Environment Variable IS Set To (NULL)
Although I hope Myenv printed "Xemacs", but because there is no export environment variable, it is not working very well. This time makes it work correctly:
$ export editor
$ ./myenv
The Editor Environment Variable Is Set To Xemacs
Now, if you see it in your own: Do not export environment variables, another process (in this example C program) does not see environment variables. By the way, if you want, you can define and export environment variables in a row, as shown below:
$ export editor = Xemacs
This is the same as the two versions of the version. Now this demonstrates how to use unset to remove environment variables:
$ unset editor
$ ./myenv
The Editor Environment Variable IS Set To (NULL)