Cu Play Shell Thirteen Questions: VAR = VALUE? Where is the difference before and after EXPORT?

xiaoxiao2021-03-06  63

Author:

This time, this time, let us temporarily throw the Command line, first understand the Bash variable ... The so-called variable is to use a specific "name" to access a variable "value" (Value). * Setting (SET) * In Bash, you can use "=" to set or redefine the variable: Name = Value When setting the variable, comply with the following rules: * Isographical symbols (IFS), should also avoid using the SHELL retention word (Meta Charactor). * Variable name cannot use the $ symbol. * The first letter of the variable name cannot be a number (NUMBER). * Variable name length should not exceed 256 letters. * Variable name and variable value are different (Case Sensitive). The following is a number of variables to set the time error: a = b: No IFS 1A = B: Can not start with a number $ A = B: Name Can't have $ A = B: This is different from A = B is different, it is possible Accepted setting: a = "b": IFS is closed (please refer to the quoting chapter in front) A1 = B: Not at the beginning A = $ B: $ can be used in variable value THIS_A_LONG_NAME = B: Available _ connection longer Name or value, and don't do something. * Substitution * SHELL is powerful, one of which is that it can be replaced with variables in the command line. In the command line, the user can use the margin to add the variable name (except that the variable name is defined by the variable name), then replace the command line again. Alterate:

Code: $ a = ls $ b = la $ c = / tmp $ $ A - $ B $ C (Note: The first $ is the last $ is the shell prompt, is not within the command line.) Must emphasize is The variable we mentioned is replaced, only the top of Command Line. (Yes, let's go back to Command Line!) Carefully analyze the last line of Command Line, it is not difficult to find that the $ symbol will replace each variable before being executed (before entering Cr characters) (before the variable) Replace the reorganization of the command line again, will finally draw the following command line:

Code: LS -LA / TMP still remembers the second chapter I invite the two sentences "Be sure to understand"? If you forget, then I will post it again:

Quote: If you come from technical details, the shell will disassemble the text entered by Command Line according to the IFS (Internal Field SEPERATOR). Then, for special characters (META), it will be processed, and finally recombine Command Line. The $ here is one of the most classic meta in Command Line, that is, the variable is replaced! In daily shell operations, we often use the echo command to view the values ​​of specific variables, for example:

Code: $ Echo $ A - $ B We have learned, the echo command is simply sent to "Standard Output" (Stdout, usually our screen). So the above command will get the following results on the screen:

Code: LS -LA / TMP This is the result of replacing $ A (LS), $ B (LA), with $ C (/ TMP) when the echo command is executed. With the replacement of the variable, we are more flexible when setting variables: A = B b = $ A This, the variable value of B can inherit the variable value of the A variable "at the time". However, do not use "mathematical Luo" to set the variable setting, rather than saying: A = B = C does not make A of the variable value into c. Another example is: A = B = $ A A = C, the value of B is also changed to C. The above is a variable that is simply defined by two different names: A and B, and their values ​​are B and C. If the variable is repeatedly defined, the original old value will be replaced by the new value. (This is not "variable amount"? ^ _ ^) When we set a variable, please remember this: * Save a value with a name. In addition, we can also use the command line variable replacement capability to "expand" (Append) variable: a = B: C: DA = $ A: E, the first line we set a value of A "B: C: D ", then, the second line will then expand the value as" A: B: C: E ". The above expansion example, we use the interval symbol (:) to achieve the expansion, if there is no interval symbol, if there is a problem: a = bcd a = $ AE Because the second is to inherit a value of a $ AE The result of the change, not $ A and add E! To resolve this issue, we can use more rigorous replacement processing: a = BCD a = $ {a} E in the previous example, we use {} to clearly define the range of variable names, so we can put A The variable value is expanded from BCD to BCDE. (Tip: About $ {Name} The fact that can also do more variable processing power, these all belong to the advanced variable processing, the current stage is not introduced, please refer to the information. Such as CU's post: http://www.chinaunix.net/forum/viewtopic.php?t=201843) * Export * Strictly, we are in the current shell, all of which belong to "local variable", only through The "Output" processing of the export command can be an environment variable: Code: $ a = b $ export a or:

Code: $ export a = b After the export output is processed, the variable A can become an environment variable for future commands. When using Export, please don't forget shell's "Substitution" on the command line, and say:

Code: $ a = b = c $ export $ A The command is not output to an environment variable, but the B is output because of this command line, $ A will be first either B and then "stuff" as an export parameter. To understand this export, in fact, it is necessary to understand from the perspective of Process. I will explain the concept of Process, so please pay attention to you. * Cancel variable * To cancel a variable, you can use the unset command in BASH: Code: Unset A is the same as Export, the unset command line also makes a variable replacement (this is actually one of the features of the shell), so:

Code: $ a = b $ b = C $ unset $ A The variable canceled is B instead a. In addition, once the variable is canceled after the unset, the result is to take the entire variable, not only to cancel the variable value. The next line is actually very different:

Code: $ a = $ unset A first line only sets the variable A to "null value", but the second line makes the variable A not exist. Although in the eyes of the eye, the state of these variables is the same in the following command:

Code: $ a = $ Echo $ A $ unset a $ Echo $ A To identify NULL VALUE and UNSET, this is strict in some advanced variable processing. for example:

Code: $ str = # Set to Null $ VAR = $ {str = expr} # Defining Var $ Echo $ VAR $ ECHO $ ​​STR $ unset str # Cancel $ var = $ {str = evr} # definition var $ echo $ var Expr $ Echo $ STR EXPR Smart Reader (Yes, you!), slightly thinking, it should be difficult to find why the same var = $ {str = expr} is different under NULL and Unset? If you can't see it, it may be one of the following reasons: a. You are too stupid B. Don't understand the advanced processing C. This article has not been obtained, I haven't been digested. I don't know, which one do you choose? .... ^ _ ^

转载请注明原文地址:https://www.9cbs.com/read-89241.html

New Post(0)