Shell Tutorial Shell echo command
Shell variables
When defining a variable, the variable name does not have a dollar sign (required for variables in the $,php language), such as:
Your_name= "w3cschool.cc"
Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the name of the variable names must follow the following rules:
The first character must be a letter (a-z,a-z).
You can use an underscore (_) without spaces in the middle.
Punctuation cannot be used.
You can't use the keywords in bash (you can see the reserved keywords using the help command).
In addition to explicitly assigning values directly, you can use statements to assign values to variables, such as:
For file in ' ls/etc '
The above statement loops out the file name of the directory under/etc.
Using variables
With a defined variable, just precede the variable name with a dollar sign, such as:
Your_name= "QINJX" Echo $your _nameecho ${your_name}
The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:
For skill in Ada coffe Action Java do echo ' I am good at ${skill}script ' done
If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null) and the result of the code execution is not what we expect it to look like.
It is a good programming habit to add curly braces to all variables.
A defined variable can be redefined, such as:
Your_name= "Tom" Echo $your _nameyour_name= "Alibaba" Echo $your _name
This is legal, but note that the second assignment can not be written $your_name= "Alibaba", the use of variables when the dollar symbol ($).
Shell string
Strings are the most common and useful data types in shell programming (except numbers and strings, and no other type works well), strings can be in single quotes or double quotes, or without quotes. The difference between single and double quotes is similar to PHP.
Single quotation marks
Str= ' This is a string '
Single-Quote String restrictions:
Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;
Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).
Double quotes
Your_name= ' QINJX ' str= "Hello, I know your is \" $your _name\ "! \ n "
Advantages of double quotes:
Stitching strings
Your_name= "QINJX" greeting= "Hello," $your _name "!" greeting_1= "Hello, ${your_name}!" echo $greeting $greeting _1
Get string length
string= "ABCD" Echo ${#string} #输出 4
Extract substring
String= "Alibaba is a great company" Echo ${string:1:4} #输出liba
Finding substrings
String= "Alibaba is a great company" Echo ' Expr index "$string" is "
Shell Array
Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays.
Similar to the C language, the subscript of an array element is numbered starting with 0. Gets the elements in the array to take advantage of subscript, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0.
Defining arrays
In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space" symbol. The general form of the definition array is:
Array name = (value 1 value 2 ...) Value N)
For example:
Array_name= (value0 value1 value2 value3)
Or
Array_name= (VALUE0VALUE1VALUE2VALUE3)
You can also define individual components of an array individually:
Array_name[0]=value0array_name[1]=value1array_name[n]=valuen
You can not use successive subscripts, and there is no limit to the range of subscripts.
Reading an array
The general format for reading array element values is:
${array name [subscript]}
For example:
Valuen=${array_name[n]}
Use the @ symbol to get all the elements in the array, for example:
Echo ${array_name[@]}
Gets the length of the array
The method of getting the length of the array is the same as getting the string length, for example:
# Gets the number of array elements length=${#array_name [@]}# or length=${#array_name [*]}# Gets the length of the single element of the array lengthn=${#array_name [n]}
Shell annotations
Lines that begin with "#" are comments, which are ignored by the interpreter.
There is no multiline comment in sh, only one # is added to each line. Can only be like this:
#--------------------------------------------# This is a script that automatically plays IPA, based on Webfrogs's ipa-build writing: # https://github.com/ webfrogs/xcode_shell/blob/master/ipa-build# Features: Automatic packaging for Etao iOS app, 14 channels of Output IPA package # Features: Fully automatic packaging, no need to enter any parameters #-------------- ------------------------------##### User Configuration area to start ######## the project root directory, it is recommended to put this script at the root of the project, there is no need to change the # application name, to ensure and Xcode product under the target_ Name.app name consistent ###### User Configuration area End #####
What if, in the course of development, you encounter a large segment of code that needs to be annotated temporarily and then uncomment later?
Each line with a # symbol is too laborious, you can put this piece of code to be annotated with a pair of curly braces, defined as a function, there is no place to call this function, the code will not be executed, to achieve the same effect as the annotation.
Shell Tutorial Shell echo command
Shell Variable Learning Notes