Shell Basics Learning (ii) Shell variables

Source: Internet
Author: User

1. Data type:

String

Array

2. Naming conventions for variables:

Can only start with a-Z or a-Z

Cannot have spaces in the middle, you can use _

Punctuation cannot be used

Cannot use the SHELL keyword

3. Variable type:

Environment variables

Local variables

Shell variables

4. 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 quotes:

Output exactly as quoted, invalid internal variable, invalid escape character

Double quotes:

Internal variables can be parsed, escape characters can appear

Stitching characters

Name='liupf'echo"$name  Echo " Hello ${name} "    

Get string length

Name="liupf"    echo ${#name}

Intercept string

$name ='liupf'    echo ${name:1:3}

Find sub-string location

    string=My name is LIUPF  echo 'expr'$string" Is  '

Array

1, the shell uses () to represent the array, the elements are separated by a space. The basic form is expressed as:

A, array name = (value 0 value 1 value 2 value N)

B, array name = (

Value 0

···

Value n

C, array name [0]= value 0

Array name [...] =··· Value

Array name [n]= value 2

2. Reading the value of an array

${array name [table below]}

3. Get all the values of the array

${array name [@]}

4. Get the length of the array

${#数组名 {@}}

${#数组名 {*}}

${#数组名 {n}}

Shell Basics Learning (ii) Shell variables

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.