Shell supports custom variables. When defining a variable, the variable name does not contain a dollar sign ($), for example:
Copy text-only new window
- 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
- Myurl = "http://see.xidian.edu.cn/cpp/linux"
- 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
- Your_name = "mozhiyan"
- Echo $ your_name
- 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
- For skill in ADA coffe action Java
- Do
- Echo "I am good at $ {skill} script"
- 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
- Myurl = "http://see.xidian.edu.cn/cpp/linux"
- Echo $ {myurl}
-
- Myurl = "http://see.xidian.edu.cn/cpp/shell"
- 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
- #! /Bin/bash
-
- Myurl = "http://see.xidian.edu.cn/cpp/shell"
- Readonly myurl
- 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
- 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
- #! /Bin/sh
-
- Myurl = "http://see.xidian.edu.cn/cpp/u/xitong"
- Unset myurl
- 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