When you define a variable, the variable name without the dollar sign ($, PHP language variables needed), such as: your_name = " runoob.com " Note that there is a space between the variable name and the equal sign, which may be familiar to you and all programming languages They are not the same. Meanwhile, the name of the variable name must follow these rules: naming only letters, numbers and underscores, the first character can not start with a number. Excluding space, you can use an underscore (_). You can not use punctuation. Bash can not be used in keyword (help command to view the available reserved keywords). Shell valid variable names as examples: RUNOOB the LD_LIBRARY_PATH _var var2 addition to direct assignment explicitly, can also be used for variable assignment statement, such as: for File in `LS / etc` or for File in $ (LS / etc) above statement / etc directory under the file name out of circulation.
Use a variable using a variable defined earlier, as long as the dollar plus sign in front of variable name, such as: your_name = " qinjx " echo $ your_name echo $ {} your_name variable name outside the curly braces are optional, plus without all OK, add braces to help identify the boundary interpreter variables, such as the following case: for skill in Ada the Java Coffe Action; do echo " the I AM AT $ {skill} Good Script " DONE If you do not add to the skill variable flower brackets, written echo " the I AM AT $ skillScript Good " , the interpreter will put $ skillScript as a variable (its value is empty), code execution is not the result we expect the same again. Recommended for all variables braces, this is a good programming practice. Defined variables, can be re-defined, such as: your_name = " tom " echo $ your_name your_name = "alibaba " echo $ your_name write is legal, but note that when the assignment of the second your_name can not write $ = " alibaba " , using variables when it added a dollar sign ($).
Read-only variables using the readonly command variable can be defined as read-only variables, the value of read only variables can not be changed. The following examples try to change the read-only variable, the results of error: # ! / Bin / bash myurl = " http://www.google.com " Readonly myurl myurl = " http://www.runoob.com " run the script, the results as follows: / bin / SH: NAME: This variable IS the Read only.
Delete variables with the unset command to delete a variable. Syntax: unset variable_name can not be used again after the variable is deleted. unset command can not remove the read-only variable. Examples # ! / Bin / SH myurl = " http://www.runoob.com " unset myurl echo $ myurl above example will not perform any output.
Variable type when you run the shell, while there will be three kinds of variables: 1 ) Local variables Local variables defined in a script or command, only valid in the current shell instance, start the other shell program can not access local variables. 2 ) environmental variables all procedures, including the shell is started, can access environment variables, some programs require environment variables to ensure their normal operation. When necessary, the shell script can also define environment variables. . 3) is a special shell variable shell variable variable set by the shell program. shell variable environment variable part is, in part, local variables, to ensure the normal operation of the shell
Shell String String shell programming is the most common and useful type of data (except numbers and strings, but also other types of handy lacks), the string enclosed in single quotes, double quotes may be used, it may be practiced without quotes . The difference between single and double quotation marks are similar with PHP. Single quotes STR = ' the this IS A String ' restriction single quoted strings: any character in single quotes are directly output variables in single quoted strings are invalid; any character in single quotes are output as single quotes the variable string is invalid;
Double quotes the your_name field = ' runoob ' STR = " the Hello, the I know are you \" $ the your_name field \ "\ n-! " Echo - E $ STR output is: the Hello, the I know you are " runoob " ! Advantage double quotation marks: double quotes can have variable double quotes may appear escape character
String concatenation the your_name field = " runoob " # double quotes splice Greeting = " Hello, " $ the your_name field " ! " Greeting_1 = " Hello, the your_name field $ {}! " Echo $ $ greeting_1 Greeting # single quotes splicing greeting_2 = ' Hello, ' $ the your_name field ' ! ' greeting_3 = ' ! Hello, the your_name field $ {} ' echo $ $ greeting_3 greeting_2 output is: Hello, runoob Hello, runoob!! Hello, runoob Hello, the your_name field} $ {!!
Get string length String = " ABCD " echo $ { # String} # 4 outputs the extracted substring following examples from the string of two characters taken beginning 4 characters: String = " runoob IS Great Site A " echo $ {String : 1: 4} # output unoo find substring to find the location of a character or o i (which letter comes first calculates what): string = " runoob iS Great Site a " echo `expr index " $ string " io` # output 4 Note: the above script `anti quotes, rather than single quotes '
Shell array bash support one-dimensional array (not support multi-dimensional array), and do not limit the size of the array. Similar to the C language, the subscript of the array elements are numbered starting from zero. To obtain the use of elements in the array subscript or index may be an integer arithmetic expression whose value is greater than or equal to 0. Array defined in the Shell, with parentheses for the array, with the array elements " space " divided symbols. The general form of an array defined as: array name = (value 1 value 2 ... value n) , for example: ARRAY_NAME = (VALUE1 value0, value2 value3) or ARRAY_NAME = ( value0, VALUE1 value2 value3 ) can also define an array of separate individual components: ARRAY_NAME [0] = value0, ARRAY_NAME [ . 1] = VALUE1 ARRAY_NAME [n-] = valueN may not be continuous subscripts and subscript range is not limited.
Array read general format array element value is read: $ {array name [index]} example: valueN = $ ARRAY_NAME {[n-]} @ sign have access to all elements in the array, for example: echo $ {ARRAY_NAME [@]} to get the length of the array acquiring array length method as that of obtaining the same string length, for example: # acquired array element number length = $ { # ARRAY_NAME [@]} # or length = $ { # ARRAY_NAME [ *]} # acquired length of the array of individual elements lengthn = $ { # ARRAY_NAME [n-]}
To # beginning of the line is a comment and will be ignored interpreter. Each row by adding a # number is set multi-line comments, like this: # ---------------------------------- ---------- # this is a comment multi-line comment multi-line comments can also use the following format: : << EOF annotation content ... annotation content ... annotation content ... EOF EOF can use other symbols: : << ' comments contents ... the comment content ... footnotes ... ' : << ! footnotes ... annotation content ... footnotes ... !