1.Shell itself is a program written in C language, it is a bridge for users to use Unix/linux, most of the user's work is done through the shell. the shell is both a command language and a programming language. as a command language, it interprets and executes user-entered commands interactively, and as a programming language, it defines variables and parameters and provides many control structures in high-level languages, including loops and branches.
2.the shell has two ways of executing commands:
- Interactive (Interactive): Explains the execution of a user's command, the user enters a command, and the shell interprets the execution of a single rule.
- Batch: The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time.
Shell scripts are similar to programming languages, and there are variables and process control statements, but shell scripts are interpreted and do not need to be compiled, and the shell program reads and executes the commands from a single line of script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution.
3.bash is fully compatible with SH, meaning that a script written in SH can be executed without modification in bash.
4.Shell supports custom variables.
when defining a variable, the variable name does not have a dollar sign ($), such as:
- VariableName = "Value"
Note that there can be no spaces between the variable name and the equals sign (there are no spaces on either side), which may be different from any programming language you are familiar with. at the same time, the name of the variable names must follow the following rules:
- You cannot have spaces between them, you can use underscores (_).
- Punctuation cannot be used.
- You can't use the keywords in bash (you can see the reserved keywords using the help command).
5. using a defined variable, simply precede the variable name with a dollar sign ($) , such as:
- your_name = "Mozhiyan"
- $your _name
- ${your_name} }
the curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable , such as the following:
- for inch
- Do
- "I am Good at ${skill}script"
- Done
If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null)and the result of the code execution is not what we expect it to look like.
It is a good programming habit to add curly braces to all variables.
6. Read-only variables
Use the
readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
The following example attempts to change a read-only variable, resulting in an error:
- #!/bin/bash
- Myurl = "http://see.xidian.edu.cn/cpp/shell/"
- ReadOnly
- Myurl = "http://see.xidian.edu.cn/cpp/danpianji/"
run the script with the following results:
/bin/sh:name:this variable is read only.
7. Deleting variablesUse the
unset command to delete a variable. Syntax:
- unset variable_name
The variable cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.
Linux Shell Learning