Shell Programming--variables

Source: Internet
Author: User

First, define variables

Define variable names without a $ (unlike PHP, as with Python), with no spaces between variable names and "=" (special, unlike other languages)

Example: x= "Hello World"

Note: The naming of variable names should 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).

Referencing a defined variable requires the addition of a $ character

Such as:

x= "Hello World"

Echo ${x}

Second, read-only variables

Defined with the ReadOnly command, such as:

x= "Hello World"

ReadOnly x

The value of x cannot be changed at this time

Third, delete the variable

Use the unset command to delete a variable, but the unset command cannot delete a read-only variable

Iv. getting the string length

string= "ABCD"

echo ${#string} #输出 4

Five, find sub-characters

String="Runoob is a great company"

echo ' expr index ' $string ' is ' # output 8

Six, extracting substrings

The following instance intercepts 4 characters starting with the 2nd character of the string:

String="Runoob is a great site"

echo ${string:1:4} # output Unoo

Seven, reading the array

Valuen=${array_name[n]}

Read all the elements in the array:

Use the @ sign, echo ${array_name[@]}

Shell Programming--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.