Linux variable learning experience
I learned about Linux variables some time ago. Now, there are three points to note:
- Differences between Linux variables and C/C ++ Variables
- Linux variable reference
- Linux variable-specific command replacement
- First, Linux variables are more like macro definitions, but they can be changed to the content. That is to say, in C/C ++, variables can only be assigned to different types of data, such as int, char, and ptr. However, in Linux, variables can be assigned in any form, such as strings. For this reason, variables are more like macro definitions in C/C ++.
Because the variables contain different content, the variables referenced in Linux and C/C ++ are naturally different. In Linux, there are two forms of variable reference:
- $ Var: This method is common and simple. However, we recommend that you use the following method to accurately express and reduce the reading difficulty;
- $ {Var.
Note: There are two ways to assign values to variables:
- Single quotes '': The content in single quotes is assigned to the variable, even if it contains other variables;
- Double quotation mark "": the variable reference in double quotation marks is replaced with the actual content of the variable.
- The command to replace variables is in the format of $ (commend). In essence, the result of commend execution is assigned to another variable as the content or used as another variable.
Here we will talk about some in-depth analysis of command replacement (personal opinions ):
- Relationship between command replacement and MPs queue:
- For a pipeline, it is the relationship between two commands (processes) and must meet three conditions:
- The previous command must be correctly executed;
- The previous command can generate standard out information;
- The last command can receive information parameters.
Then, the result of the previous command can be passed to the next command, and the behavior can only be between two commands.
- For command replacement, it does not have the constraints of these conditions. As long as the command is executed, whatever the result is, the running result will be taken out and assigned to another variable or another user.
- Relationship between command replacement and alias:
- For alias, it can be used as an alias for a command, but it is actually another name. Similar to typedef, it represents the command itself rather than the result of its execution;
- For command replacement, it gets the result of command execution rather than the command itself.
This article permanently updates the link address: