2-shell variable

Source: Internet
Author: User
Tags php language

When defining a variable, the variable name does not have a dollar sign (required for variables in the $,php language), such as:
Your_name= "Runoob.com"
Attention There can be no spaces between the variable name and the equals sign, which may not be the same as any programming language you are familiar with. At the same time, the name of the variable names must follow the following rules:
Names can only use letters, numbers, and underscores, and the first character cannot begin with a number.
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).
Examples of valid Shell variable names are:
Runoob
Ld_library_path
_var
Var2
Invalid variable name:
? var=123
User*name=runoob
In addition to explicitly assigning values directly, you can use statements to assign values to variables, such as:
For file in' ls/etc '
Or
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:
   #!/bin/sh
Your_name= "QINJX"
Echo $your _name
Echo ${your_name}
Outside of the variable nameCurly BracesIsoptions available, plus the curly braces to help the interpreter identify the bounds of the variable, such as the following:
   #!/bin/sh
For skill inAda 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:
#!/bin/sh
Your_name= "Tom"
Echo $your _name
Your_name= "Alibaba"
Echo $your _name
This is legal, but note that the second assignment cannot be written $your_name= "Alibaba",the dollar symbol ($) is used when using variables.

read-only variables
Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
The following example attempts to change a read-only variable, resulting in an error:
#!/bin/bash
Myurl= "http://www.w3cschool.cc"
ReadOnly Myurl
Myurl= "Http://www.runoob.com"
Run the script with the following results:
/bin/sh:name:this variable is read only.

Delete a variable
Use the unset command to delete a variable. Grammar:
Unset variable_name
The variable cannot be used again after it has been deleted. The unset command cannot delete a read-only variable.
   #!/bin/sh
Myurl= "Http://www.runoob.com"
Unset Myurl
Echo $MYURL
The above instance execution will have no output.

Variable Type
When you run the shell, there are three different variables:
1)Local VariablesLocal variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
2)Environment VariablesAll programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
3)Shell VariablesShell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that ensure the shell's normal operation '

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 quote willoutput AS-is, in a single-quote stringthe variable is not valid;
In single quote stringcannot appear single quotation marks(It does not work with the escape character for single quotes).
Double quotes
Your_name= ' QINJX '
Str= "Hello, I know your is \" $your _name\ "! \ n "
Advantages of double quotes:
In double quotescan have variables
In double quotesescape characters can appear
Stitching Strings
Your_name= "QINJX"
greeting= "Hello," $your _name "!"
greeting_1= "Hello, ${your_name}!"
echo $greeting $greeting _1
Get string length
  #!/bin/sh
string= "ABCD"
Echo ${#String#输出 4
Extract substring
The following instance intercepts 4 characters starting with the 2nd character of the string:
   #!/bin/bash
String= "RUnooB is a great site "
Echo ${string: 1:4}# output Unoo

The following instance intercepts 4 characters starting with the 1th character of the string:
   #!/bin/sh
String= "ABCDEFG"
Echo ${string} | Cut-c 1-4 # output ABCD
Finding substrings
Find the location of the character "I or S":
   #!/bin/sh
String= "Runoob isA great company "
Echo' expr index ' $string ' is ' # Output 8 # This must be ""!
Note: the "'" in the above script is an anti-quote, not a single quote "'", don't look wrong.

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= (
Value0
Value1
value2
Value3
)
You can also define individual components of an array individually:
Array_name[0]=value0
Array_name[1]=value1
Array_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 an array ofall elementsFor 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 an array of individual elements
   #!/bin/bash
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 comment
# Author: Rookie Tutorial
# site:www.runoob.com
# Slogan: Learn not only the technology, but also the dream!
#--------------------------------------------
##### User Configuration area starts #####
#
#
# Here you can add script description information
#
#
##### end of User Configuration area #####
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.













2-shell variable

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.