Shell programming variables a bash variable and variable classification
Bash is a standard shell in Linux, so it's often called bash as a shell.
1. Variable naming rules
- Variable names can consist of letters, numbers, underscores, but must begin with a letter and an underscore;
- The length of the variable name cannot exceed 255 characters;
- Variable names must be unique in the valid range;
- In bash, the default types of variables are string-type;
2. Variables are categorized by storage type
The default type is a string type.
- String type, the value of single-double quotation marks can be;
- Plastic
- floating point type;
- Date type;
Two-User custom variables
Variable names and variable values are user-defined, meaning they can be modified by user control.
1. Variable definition
Variable name = variable Value
Note : "=" cannot have spaces around.
2. Variable invocation
ECHO is the simplest variable invocation and can only be used as output.
such as: Echo $ variable Name
3. Variable Overlay
For example:
X=123
x= "$x" 456
x=${x}789
Output 123456789 when echo $x
Useful: Add environment variables, such as path paths.
4. Variable view
Set
Ability to query all variables currently running on the system, including system environment variables and user-defined variables for the current shell.
Option:-U, if you set this option, and then use Echo to invoke an undeclared variable, you will get an error.
If the y variable is not declared assigned, a blank line is printed when you use the Echo $y, and if you first use Set-u, then echo $y will prompt for the variable.
5. Variable deletion
unset
How to use: unset x,unset does not delete the value inside the variable, but instead deletes the variable, so the variable name does not need to be added $.
Three environment variables
The bash command can be used to enter the current Shell's child shell, you can view the current shell through Pstree, this method can be used to do the following environment variables and user-defined variables in the effective area of the experiment.
1. Differences between environment variables and user-defined variables
Environment variables are global variables, and user-defined variables are local variables.
User-defined variables take effect only in the current shell, and the environment variables take effect in the current shell and all child shells of the shell.
The user can customize the environment variables, but the environment variable names and variables that are in effect for the system are fixed.
2. User-defined environment variables
$ export Variable name = variable Value
or $ variable name = variable Value
For example: $ export Variable name
3. View Environment variables
$ set View all variables
$ ENV View Environment variables
4. Delete environment variables
$ unset environment variable
5. PS1 Environment variables
Chapter II Variables of Shell programming