Shell tutorial 2-Variables

Source: Internet
Author: User
Shell supports custom variables. When defining a variable, the variable name does not contain a dollar sign ($), for example:
Copy text-only new window
 
  1. Variablename = "value"
variableName="value"
Note that there is no space between the variable name and equal sign, which may be different from all programming languages you are familiar. Variable names must follow the following rules:
  • The first character must be a letter (a-Z, A-Z ).
  • There cannot be spaces in the middle, and you can use underscores (_).
  • Punctuation cannot be used.
  • You cannot use keywords in bash (you can use the help command to view reserved keywords ).

Example of variable definition:
Copy text-only new window
 
  1. Myurl = "http://see.xidian.edu.cn/cpp/linux"
  2. Mynum = 100
myUrl="http://see.xidian.edu.cn/cpp/linux/"myNum=100
To use a variable with a defined variable, you only need to add the dollar sign ($) before the variable name, such:
Copy text-only new window
 
  1. Your_name = "mozhiyan"
  2. Echo $ your_name
  3. Echo $ {your_name}
your_name="mozhiyan"echo $your_nameecho ${your_name}
The curly braces outside the variable name are optional and can be added without adding them. The curly braces are used to help the interpreter identify the boundary of the variable, for example:
Copy text-only new window
 
  1. For skill in ADA coffe action Java
  2. Do
  3. Echo "I am good at $ {skill} script"
  4. Done
for skill in Ada Coffe Action Java do    echo "I am good at ${skill}Script"done
If you do not add curly brackets to the skill variable and write it as Echo "I am good at $ skillscript", the interpreter regards $ skillscript as a variable (its value is null ), the code execution result is not what we expect.

We recommend that you add curly braces to all variables. This is a good programming habit. You can redefine a variable that has been defined, for example:
Copy text-only new window
 
  1. Myurl = "http://see.xidian.edu.cn/cpp/linux"
  2. Echo $ {myurl}
  3.  
  4. Myurl = "http://see.xidian.edu.cn/cpp/shell"
  5. Echo $ {myurl}
myUrl="http://see.xidian.edu.cn/cpp/linux/"echo ${myUrl}myUrl="http://see.xidian.edu.cn/cpp/shell/"echo ${myUrl}
This write is legal, but note that the second value cannot be written $ myurl = "http://see.xidian.edu.cn/cpp/shell/", the dollar sign ($) is used when the variable is used ). Read-Only variable usage ReadonlyYou can define a variable as a read-only variable. The value of a read-only variable cannot be changed.

In the following example, an error is returned when you try to change the read-only variable:
Copy text-only new window
 
  1. #! /Bin/bash
  2.  
  3. Myurl = "http://see.xidian.edu.cn/cpp/shell"
  4. Readonly myurl
  5. Myurl = "http://see.xidian.edu.cn/cpp/danpianji"
#!/bin/bashmyUrl="http://see.xidian.edu.cn/cpp/shell/"readonly myUrlmyUrl="http://see.xidian.edu.cn/cpp/danpianji/"
Run the script. The result is as follows:
/bin/sh: NAME: This variable is read only.
Delete variable usage UnsetCommand to delete a variable. Syntax:
Copy text-only new window
 
  1. Unset variable_name
unset variable_name
After a variable is deleted, it cannot be used again. The unset command cannot delete Read-Only variables.

For example:
Copy text-only new window
 
  1. #! /Bin/sh
  2.  
  3. Myurl = "http://see.xidian.edu.cn/cpp/u/xitong"
  4. Unset myurl
  5. Echo $ myurl
#!/bin/shmyUrl="http://see.xidian.edu.cn/cpp/u/xitong/"unset myUrlecho $myUrl
The above script has no output. When you run shell for the variable type, there are three variables at the same time:
1) local variables are defined in scripts or commands and only valid in the current shell instance. Other programs started by shell cannot access local variables. 2) All programs with environment variables, including those started by shell, can access environment variables. Some programs require environment variables to ensure normal operation. When necessary, shell scripts can also define environment variables. 3) The shell variable is a special variable set by the shell program. Some shell variables are environment variables and some are local variables, which ensure the normal operation of shell.

Shell tutorial 2-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.