Use of variables in the shell

Source: Internet
Author: User

1. Declaration of variables

Name= "Blacksonny"

 Note://variable definition does not add $, the variable and equal sign cannot have spaces between

Variable naming 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 with the help command)

2. Using variables

If you want to use a variable, add $ to it before the variable, and we recommend using ${name}

such as: $name

1 #!/bin/bash2#description: Use variable3#Author: Blacksonny 4 #Copyright (c) 5 #Script follows here: 6 7 name="blacksonny"8echo  $name  9echo ${name}

The above code then outputs Blacksonny at the same time, where {} is optional and is generally used to define the bounds of the variable

The following code distinguishes between variables and programm, and no person can get the value of a variable

 for inch Java Php Android IOS  do    Echo " I'll study ${book}programm "  Done

3, the variable is redefined, the shell allows to redefine the variable, as with the method of defining the variable, just re-assign the original variable value can be

4. read-only variables

Define a read-only variable by using the ReadOnly keyword

number=1323readonly numbernumber=1333

The run var.sh result is: "./var.sh: Line 24:number: read-only variable", prompting that this variable is not allowed to be modified.

5. Delete variables

Use unset var_name to delete variables

6. Variable type

1) Local Variables

2) Environment variables

3) Shell variables

Use of variables in the shell

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.