1 #! /Bin/bash 2 3 # variable assignment and replacement 4 5 A = 375 6 Hello = $ A 7 8 # --------------------------------------------------------------------- 9 # note that when assigning values, do not have spaces before and after the equal sign. 10 # What if there is a space? 11 12 # "variable = value" 13 # ^ 14 # % the script will try to run a "variable" command with a "= value" parameter. 15 16 # "variable = value" 17 # ^ 18 # % the script will try to run a "value" command, 19 # + with an environment variable assigned as "variable ". 20 # ------------------------------------------------------------------------- 21 22 23 echo hello # No variable reference, just a hello string. 24 25 echo $ Hello 26 echo $ {Hello} # Same as above. 27 28 echo "$ hello" 29 echo "$ {Hello}" 30 31 echo 32 33 Hello = "a B c d" 34 echo $ hello # a B C D 35 echo" $ hello "# a B c d 36 # As you can see, Echo $ hello and echo" $ hello "give different results. 37 #===================================================== =======================================38 # blank space will be retained when a variable is referenced, of course, if the variable is replaced, it will not be retained. 39 #======================================================== =======================================40 41 echo 42 echo '$ hello' # Hello 43 echo' $ hello '# $ Hello 44 # ^ 45 # the function of full reference will cause "$" to be interpreted as a separate character, 46 # + instead of the variable prefix. 47 48 # note the different effects of the two references. 49 50 51 Hello = # Set to null. 52 echo "\ $ Hello (null value) = $ hello" 53 # note that setting a variable to null matches the unset variable, 54 # + although the final result is the same (see below for details ). 55 56 # ------------------------------------------------------------ 57 58 # multiple variables can be set on the same line, 59 # +, but must be separated by blank spaces. 60 # use it with caution. This operation reduces readability and cannot be transplanted. 61 62 var1 = 21 var2 = 22 var3 = $ V3 63 echo 64 echo "var1 = $ var1 var2 = $ var2 var3 = $ var3" 65 66 # "sh" in earlier versions" may cause problems. 67 68 # -------------------------------------------------------------- 69 70 echo; echo 71 72 numbers = "One Two Three" 73 # ^ 74 other_numbers = "1 2 3" 75 # ^ 76 # If the variable is blank, if there is whitespace embedded within a variable, 77 # +, a reference must be added. 78 # other_numbers = 1 2 3 # an error message is displayed. 79 echo "numbers = $ numbers" 80 echo "other_numbers = $ other_numbers" # other_numbers = 1 2 3 81 # However, you can also escape the blank space. 82 mixed_bag = 2 \ --- \ Whatever 83 # ^ space (\) behind the escape character (\). 84 85 echo "$ mixed_bag" #2 --- whatever 86 87 echo; echo 88 89 echo "uninitialized_variable = $ uninitialized_variable" 90 # The uninitialized variable is null (that is, there is no value ). 91 uninitialized_variable = # declaration, but this variable is not initialized. 92 # + serves the same purpose as setting the front side to a null value. 93 echo "uninitialized_variable = $ uninitialized_variable" 94 # It is still a null value. 95 96 uninitialized_variable = 23 # assign a value. 97 unset uninitialized_variable # unset this variable. 98 echo "uninitialized_variable = $ uninitialized_variable" 99 # It is still a null value. 100 echo101 102 exit 0