The best Shell? Variable Shell Variables Tutorial In 2024, In this tutorial you can learn Shell string,Shell array,Shell Notes,
When you define a variable, the variable name without a dollar sign ($, PHP language variables needed), such as:
your_name="w3write.com"
Note that there is no space between the variable name and equal sign, which may be familiar to you and all programming languages are not the same. Meanwhile, the variable name must follow these naming rules:
In addition to explicitly direct assignment, you can also assign values to variables with statements such as:
for file in `ls /etc`
The above statement the file name / etc directory out of circulation.
Use a defined variable, as long as the variable name preceded by a dollar sign, like this:
your_name="qinjx" echo $your_name echo ${your_name}
Variable name outside the curly braces are optional, plus without all right, add braces to help explain the boundary identification variables, such as the following case:
for skill in Ada Coffe Action Java; do echo "I am good at ${skill}Script" done
If you do not give skill variables plus braces, written echo "I am good at $ skillScript", the interpreter will put $ skillScript as a variable (its value is empty), code execution is not the result we expect the same again.
Tell all the variables braces, it 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 second assignment can not write $ your_name = "alibaba", use a variable when it is added to the dollar sign ($).
Use the command readonly variables can be defined as read-only variable, the value of read-only variables can not be changed.
The following examples try to change the read-only variables, the results being given:
#!/bin/bash myUrl="" readonly myUrl myUrl="http://www.w3write.com"
Run the script with the following results:
/bin/sh: NAME: This variable is read only.
Use the unset command to delete a variable. grammar:
unset variable_name
Variable can not be reused after being removed. unset command can not remove the read-only variables.
Examples
#!/bin/sh myUrl="http://www.w3write.com" unset myUrl echo $myUrl
Examples of the implementation of the above will have no output.
When you run the shell, there will be three parameters simultaneously:
String shell programming is the most common and useful data types (except numbers and strings, also lacks other types of handy), strings can be enclosed in single quotes, double quotes can also be used, or may not be in quotation marks. The difference between single and double quotation marks with PHP is similar.
str='this is a string'
Single-quoted strings restrictions:
your_name='qinjx' str="Hello, I know your are \"$your_name\"! \n"
Double quotes advantages:
your_name="qinjx" greeting="hello, "$your_name" !" greeting_1="hello, ${your_name} !" echo $greeting $greeting_1
string="abcd" echo ${#string} #输出 4
The following examples from the first two character strings start capturing four characters:
string="w3big is a great site" echo ${string:1:4} # 输出 unoo
Find character "i or s" position:
string="w3big is a great company" echo `expr index "$string" is` # 输出 8
Note: The above script "` "anti-quotes rather than single quotes" ' "Do not be deceived, oh.
bash supports one-dimensional array (does not support multidimensional arrays), and does not define the size of the array.
Similar to the C language, the subscript of array elements from 0 start number. Gets an array of elements to take advantage of the subscript, the subscript may be an integer or arithmetic expression whose value should be greater than or equal to 0.
In the Shell, use parentheses to denote an array, the array elements with the "space" symbols separated. The general form of an array is defined as follows:
数组名=(值1 值2 /en. 值n)
E.g:
array_name=(value0 value1 value2 value3)
or
array_name=( value0 value1 value2 value3 )
You can also define separately for each component of the array:
array_name[0]=value0 array_name[1]=value1 array_name[n]=valuen
You can not use a continuous index, and there is no limit under the target range.
The general format is read array element value:
${数组名[下标]}
E.g:
valuen=${array_name[n]}
Use the @ symbol can get an array of all the elements, such as:
echo ${array_name[@]}
Gets an array of lengths of string length and get the same way, for example:
# 取得数组元素的个数 length=${#array_name[@]} # 或者 length=${#array_name[*]} # 取得数组单个元素的长度 lengthn=${#array_name[n]}
Line with "#" at the beginning of the comment, will be ignored by the interpreter.
sh there are no multi-line comments, each line can only add a # sign. Only like this:
#-------------------------------------------- # 这是一个注释 # author:本教程 # site:www.w3write.com # slogan:学的不仅是技术,更是梦想! #-------------------------------------------- ##### 用户配置区 开始 ##### # # # 这里可以添加脚本描述信息 # # ##### 用户配置区 结束 #####
If the development process, encounter large chunks of code need to temporarily comment up after a while and uncommented, how to do it?
Each row add a # symbol too laborious, you can put some code to be annotated with a pair of curly brackets, defined as a function, no place to call this function, this code will not be executed, and reached as annotations Effect.