Description of variables and parameters the name of a variable to be replaced is the place where the value of the variable is saved. the value of the referenced variable is called the replacement of the variable. $ this symbol is like a sign that allows us to distinguish the name and value of a variable carefully. if variable is the name of a variable, $ variable is... description of variables and parameters the name of a variable to be replaced is the place where the value of the variable is saved. the value of the referenced variable is called the replacement of the variable. "$" this symbol is like a sign that allows us to distinguish the name and value of a variable carefully. if variable is the name of a variable, $ variable refers to the value of the variable, that is, the data contained in the variable. [html] root @ ubuntu :~ # Variable = 12 root @ ubuntu :~ # Echo variable root @ ubuntu :~ # Echo $ variable 12 when the variable has no $ prefix, the variable may have the following situations. 1. variable is declared or assigned a value, 2. variable is unset, 3. variable exporte, 4. A variable is in a special situation. a variable represents a signal variable that can be assigned a value using = (such as var1 = 27 ), you can also assign values in the read command or loop header (for var2 in 1 2 3 ). replace variables enclosed by a pair of double quotes ("") will not be blocked. therefore, double quotation marks are called partial references, and sometimes referred to as "weak references ". however, if you use single quotes (''), variable replacement will be disabled, and the variable name will only be interpreted as the meaning of the word, without the replacement of the variable. therefore, single quotes are called Full quotes and sometimes referred to as "strong Quotes ". note that $ variable is actually a short form of $ {variable. in some contexts, $ variable may cause errors. in this case, you need to use $ {variable }. Instructions [html] #! /Bin/bash # assign values to and replace variables a = 375 # No $ Here is declared and assigned hello = $ a # $ a to use the value of variable, assign this value to the hello variable # --------------------------------------------------------------------------- # note that there must be no spaces before and after the equal sign when assigning values. # What if there is a space? # "VARIABLE = value" # The script will try to run a "VARIABLE" command with a "= value" parameter. # "VARIABLE = value" # The script will try to run a "value" command with an environment VARIABLE "VARIABLE" assigned ". # define echo hello # No $ print the name of the hello variable echo $ hello # print the value of the hello variable echo $ {hello} # print the value of the hello variable echo "$ hello" echo "$ {hello}" hello = "a B c d" # reassigning echo $ hello # a B C D. referencing A variable will keep the blank space, of course, if the variable is replaced, it will not be retained. Now. echo "$ hello" # a B C D echo '$ hello' hello = echo "\ $ hello (null value) = $ hello "V3 = 23 var1 = 21 var2 = 22 var3 = $ V3 echo" var1 = $ var1 var2 = $ var2 var3 = $ var3 "numbers =" one two three "other_numbers = "1 2 3" echo "numbers = $ numbers" echo "other_numbers = $ other_numbers" mixed_bag = 2 \ --- \ Whatever echo "$ mixed_bag" echo "uninitialized_variable = $ uninitialized_variable variable "= echo "uni Export = $ uninitialized_variable "uninitialized_variable = 23 unset uninitialized_variable echo" uninitialized_variable = $ uninitialized_variable "exit 0 check the execution result of this script: [html] root @ ubuntu :~ /Resource/study/shell_study #. /value-test hello 375 375 375 375 a B C D $ hello (null value) = var1 = 21 var2 = 22 var3 = 23 numbers = one two three other_numbers = 1 2 3 2 --- Whatever uninitialized_variable = an uninitialized variable will be "null" -The value is not assigned a value (but it does not mean that the value is 0 !). Using this variable before assigning values to a variable usually causes problems. However, when performing arithmetic operations, it is still possible to use the uninitialized variable [html] root @ ubuntu :~ /Resource/study/shell_study # echo "$ data" root @ ubuntu :~ /Resource/study/shell_study # let "data + = 5" root @ ubuntu :~ /Resource/study/shell_study # echo "$ data" 5 conclusion: an uninitialized variable has no value, but during arithmetic operations, the uninitialized variable looks as 0. this is not documented (and may not be portable. value assignment operation (no blank before and after) Because = and-eq can be used for conditional testing, do not confuse with the value assignment operation here. note: = either a conditional test or a value assignment operation can be performed, depending on the specific context. simple assignment operation example: [html] #! /Bin/bash # assign a value to a = 879 echo "The value of \" a \ "is $. "# use 'let' to assign The value let a = 16 + 5 echo" The value of \ "a \" is now $. "# in the 'for' loop (in fact, this is a pseudo value assignment): echo-n" Values of \ "a \" in the loop are: "# Adding-n indicates ignoring the line break operation at the end of string, otherwise, it will wrap for a in 7 8 9 11 do echo-n "$ a" done echo # Use the 'read' command to assign values (this is also a type of value assignment ): echo-n "Enter \" a \ ":" read a # read The input value echo "The value of \" a \ "is now $. "exit 0 experiment result: [html] root @ ubuntu :~ /Resource/study/shell_study #. /value-test1 The value of "a" is 879. the value of "a" is now 21. values of "a" in the loop are: 7 8 9 11 Enter "a": 121212 The value of "a" is now 121212. let's look at a slightly more complex example: [html] #! /Bin/bash a = 23 # Simple assignment echo $ a B = $ a echo $ B # Now let's make a small change (command replace). a = 'echo Hello! '# Pass the 'Echo' command result to the variable 'A' echo $ a = 'ls-L' # assign the 'ls-L' result to 'A' echo $ a # however, if no quotation marks are provided, additional tabs and line breaks in the ls results will be deleted. echo "$ a" # If quotation marks are added, the blank characters in the ls result will be retained. exit 0. check the experiment result: [html] root @ ubuntu :~ /Resource/study/shell_study # chmod 777 value-test2 root @ ubuntu :~ /Resource/study/shell_study #./value-test2 23 Hello! Total 48-rwxrwxrwx 1 root 663 2013-04-22 clear_log-rw-r -- 1 root 354 2013-04-22 data-file-Example 1 root 404 2013-04-22 05:05 for_test-rwxrwxrwx 1 root root 345 2013-04-22 parse de_file-Example 1 root 831 2013-04-22 08 para_sub-rwxrwxrwx 1 root 253 2013-04-22 show_self-contained 1 root 256 2013-04-22 test1-Example 1 roo T root 204 2013-04-22 test2-rw-r -- 1 root 59 2013-04-22 test-context-rwxrwxrwx 1 root 1221 2013-04-23 value-test-rwxrwxrwx 1 root 415 value-test1-rwxrwxrwx 1 root 439 2013-04-23 value-test2 total 48-rwxrwxrwx 1 root 663 2013-04-22 clear_log-rw-r -- 1 root 354 2013-04-22 data-file-rwxrwxrwx 1 root 40 4 2013-04-22 05:05 for_test-Example 1 root 345 2013-04-22 destination de_file-Example 1 root 831 para_sub-partition 1 root 253 show_self-contained 1 root 256 test1 -rwxrwxrwx 1 root 204 2013-04-22 test2-rw-r -- 1 root 59 2013-04-22 test-context-rwxrwxrwx 1 root 1221 2013-04-23 value-t Est-rwxrwxrwx 1 root 415 2013-04-23 value-test1-rwxrwxrwx 1 root 439 2013-04-23 value-test2 Bash variable is not distinguished type unlike other programming languages, bash does not distinguish variables from "type ". basically, Bash variables are strings. however, depending on the specific context, Bash also allows comparison and integer operations. the key factor is whether the value in the variable is only a number. next, let's take a look at the next instance. [html] #! /Bin/bash a = 2334 # integer. let "a + = 1" echo "a = $ a" # a = 2335 B = $ {a/23/BB} # Replace "23" with "BB ". echo "B = $ B" # B = BB35 declare-I B # This is not helpful even if you use the declare command. echo "B = $ B" # B = BB35 let "B + = 1" # BB35 + 1 = echo "B = $ B" # B = 1 c = BB34 echo "c = $ c "# c = BB34 d =$ {c/BB/23} # Replace" BB "with" 23 ". # This changes the variable $ d to an integer. echo "d = $ d" # d = 2334 let "d + = 1" #2334 + 1 = echo "d = $ d" # d = 2335 # What about the null variable?? E = "" echo "e = $ e" # e = let "e + = 1" # Does an arithmetic operation allow a null variable? Echo "e = $ e" # e = 1 # What if no variable is declared? Echo "f = $ f" # f = let "f + = 1" # can arithmetic operations pass? Echo "f = $ f" # f = 1 # so all the variables in Bash are of a non-differentiated type. exit 0. check whether the execution result is: [html] root @ ubuntu :~ /Resource/study/shell_study #. /int-char a = 2335 B = BB35 B = BB35 B = 1 c = BB34 d = 2334 d = 2335 e = 1 f = 1 the type of the variable is not distinguished. lucky and miserable. it allows you to be more flexible when writing scripts (but it is also enough to confuse you !), In addition, you can easily write code. however, this can easily lead to errors and lead to bad programming habits. in this way, the programmer is responsible for distinguishing the variable types in the script. bash does not differentiate variable types for you. special variable types: local variables. these variables are only visible in code blocks or functions (see local variables in functions. environment variables affect the behavior of user interfaces and shells. under normal circumstances, each process has its own "environment", which is composed of a group of variables, these variables contain information that may be referenced by the process. in this case, shell is no different from a general process. each time a shell is started, it creates shell variables suitable for its own environment variables. if you update or add a new environment variable, the shell will immediately update its own environment (in other words, the changed or added variables will take effect immediately ), all the shell sub-processes (that is, the commands executed by the shell) will inherit the environment. (Translator's note: accurate It should be a child process generated subsequently to inherit the new environment variables of the Shell, and the new environment variables won't be obtained by the running child process ). space allocated to environment variables is limited. creating too many environment variables or allocating too much space to an environment variable may cause errors. if a script needs to set an environment variable, you need to "export" these variables, that is, you need to notify the local environment of the script. this is the function of the export command. A script can only export the variable to the sub-process generated by the script, that is, it can only take effect on the commands and processes generated by the script. if the script is called from the command line, the export variable of the script cannot affect the command line environment. that is to say, sub-processes cannot influence the environment of their parent processes by using the export variable. parameters passed from the command line to the script: $0, $1, $2, $3... $0 is the name of the script file. $1 is the first parameter, $2 is the second parameter, $3 is the third parameter, and then the fourth parameter.. The location parameter after $9 must be enclosed in braces, for example, $ {10}, $ {11}, ${12 }. two special variables $ * and $ @ indicate all location parameters. let's take a look at an instance [html] #! /Bin/bash # as a use case, calling this script requires at least 10 parameters, for example :#. /scriptname 1 2 3 4 5 6 7 8 9 10 MINPARAMS = 10 echo "The name of this script is \" $0 \". "echo" The name of this script is \ "'basename $0 '\". "if [-n" $1 "] # The Test variable is referenced. then echo "Parameter #1 is $1" # escape only when referenced "#" fi if [-n "$2"] then echo "Parameter #2 is $2" fi if [-n "$3"] then echo "Parameter #3 is $3" fi if [-n "$ {10}"] # Parameters greater than $9 Must Use {. then ech O "Parameter #10 is $ {10}" fi echo "-------------------------------------" echo "All the command-line parameters are: "$ *" "if [$ #-lt" $ MINPARAMS "] then echo" This script needs at least $ MINPARAMS command-line arguments! "Fi exit 0. check the experiment result: [html] root @ ubuntu :~ /Resource/study/shell_study #. /args 1 2 3 The name of this script is ". /args ". the name of this script is "args ". parameter #1 is 1 Parameter #2 is 2 Parameter #3 is 3 ------------------------------------- All the command-line parameters are: 1 2 3 This script needs at least 10 command-line arguments! Root @ ubuntu :~ /Resource/study/shell_study #. /args 1 2 3 4 5 6 7 8 9 10 11 The name of this script is ". /args ". the name of this script is "args ". parameter #1 is 1 Parameter #2 is 2 Parameter #3 is 3 Parameter #10 is 10 ------------------------------------- All the command-line parameters are: the 1 2 3 4 5 6 7 8 9 10 11 {} tag method provides an easy way to extract parameters from the command line passed to the last position of the script. some scripts may depend on use different call names, to show different behaviors. to achieve this purpose, you generally need to check $0 in the script. because the script Only one real file name is allowed. to generate multiple names, you must use symbolic links. For more information, see the example of shift command: [html] #! /Bin/bash # use 'Shift 'to gradually access all location parameters. give the script a name, such as shft, and then pass some location parameters to the script, such :. /shft a B c def 23 skidoo until [-z "$1"] # until all the location parameters are accessed... do echo-n "$1" shift done echo exit 0 view the experiment result: [html] root @ ubuntu :~ /Resource/study/shell_study #./shift d df lsjf sldjf
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.