Shell scripts from getting started to complex second (variable)

Source: Internet
Author: User

variable names do not add dollar signs when defining variables, such as:

# Myname=sxzhou


Attention:

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:

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).

"=" cannot have spaces around.


Using variables:

You can use variables with the $ symbol in front of the variable name, such as:

# Myname=sxzhou

# echo $myname


To help the interpreter identify the bounds of a variable, you can add curly braces outside the variable name, which is optional.

# echo $ (myname)


Read-only variables:

# Myname=sxzhou


Set as read-only variable

# readonly MyName


And then give the variable another worth of words will appear read-only variable:myname prompt message


To delete a variable:

Using unset, you can delete variables that have already been defined, such as:

# unset MyName


But you cannot delete a read-only variable


Variable type:

When you run the shell, there are three different variables:

1) local variable local 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 variables all 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 variables shell 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 guarantee 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.


# Myname=sxzhou

# str= ' $myname is cool '


Output variable str is the value of $myname is cool


Single quotes:

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 :

# Myname=sxzhou

# str= "$myname is cool"

Output variable str is the value of Sxzhou is cool


Advantages of double quotes:

You can have variables in double quotes.

Escape characters can appear in double quotes


Stitching strings:

# Myname=sxzhou

# status= ' is cool '

# echo $myname $STATUS


The results of the output are Sxzhou is cool


Because status is a key word for bash, it cannot be used as a variable name.


Get string Length:

The following instance intercepts 4 characters starting with the 3rd character of the string:

# str = ' Sxzhou is cool '

# echo ${str:2:4}


Note that here is the bracket {}.


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.


To define an array:

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:

array1= (value0 value1 Valuen)

Or

Array1= (

Value0

Value1

Valuen

)


You can also define individual components of an array individually:

Array1[0]=value0

Array1[1]=value1

Array1[n]=valuen


You can not use successive subscripts, and there is no limit to the range of subscripts.


To read an array:

The general format for reading array element values is:

${array name [subscript]}


For example:

Valuen=${array1[n]}


Use the @ symbol to get all the elements in the array, for example:

Echo ${array1[@]}


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.


If there is a need to annotate a large number of rows, you can enclose the part that needs comment in a pair of curly braces, defined as a function, and there is no place to call this function, this part will not execute, the effect of the comment.


Shell scripts from getting started to complex second (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.