Bash concise tutorial -- variables, bash concise tutorial Variables
1. Preface
Bash is a popular scripting language in * nix systems. As a scripting language, variables are the basic elements of a language. In this tutorial, we will learn how variables are represented in Bash and some syntax rules related to variables.
2. Bash Variables
Bash, as a programming language, represents data through variables. Bash variables are similar in concept to other programming languages we usually use. They are all column name tags represented by characters and reference these name tags to use the values represented by variables. With a variable, We can reference the value of a memory area. The assignment operation of a variable, aside from the details, we can simply understand it as creating an association between the name tag and the memory region.
In Bash, variables are non-typed. This is very different from other strong-type languages such as C, Java, and C ++.
Code:
var=100echo "$var"=> 100var="var is not a number"echo "$var"=> var is not a number
3. replace variables
In Bash, a variable can be understood as a placeholder for the value of this variable. The process of referencing this variable can be simply understood as the process of replacing the variable. In Bash, variable replacement is done through the operator "$.
Code:
var=100echo var=> varecho $var=> 100
Looking at the example above, we can see that when a variable is referenced by the "$" symbol, the value of this variable is obtained. When the variable name is used separately, the echo command only obtains the nominal value of the variable name. Therefore, "var" and "$ var" are different.
Generally, the "$" operator + variable name is used to reference the value of a variable. However, in some special statements and commands, We can omit the "$" symbol, only the variable name is used to reference the value of the variable. For example:
- When assigning values to variables
- When declaring Variables
- In the arithmetic calculation statement represented by the "()" Statement
- When using the unset command
When we learn the theme related to the content, we will see that the above mentioned theme does not use the "$" symbol.
If a variable is included in a string enclosed by double quotation marks ("), the replacement of the variable is valid. Variable replacement in a single quotes (') string is invalid, and the variable name is used as a normal string.
Code:
var=100echo "$var"=> 100echo '$var'=> $var
When a variable in double quotation marks is replaced with a variable, if the variable name does not have a separator (usually a space) between it and other characters, the variable replacement will be ambiguous, therefore, another method of variable replacement syntax is required. use $ {var} to replace the variable.
Code:
Var = 100 echo "$ varis100" => # The output is empty because the above variable replaces the value of "varis100", which is not defined in the context, so it is an empty echo "$ {var} is100" => 100is100
When using the echo command to output a variable, pay attention to one problem. If a variable is included in a double quotation mark, the output result is different.
Code:
var="A B C D"echo $var=> A B C Decho "$var"=> A B C D
4. Variable assignment
In Bash, there are many ways to assign values to variables. Using equal signs (=) to assign values to variables is the most common method.When an equal sign is used for value assignment, both sides of the equal sign cannot contain spaces..
Code:
var=100echo "var = $var"=> var = 100
In addition to assigning values to variables using equal signs, you can also use the "let" command to assign values.
Code:
let var=100echo "var = $var"=> var = 100
In the for loop statement, you can also assign a value to the variable using the "for... in..." method.
for var in 10 20 30do echo $vardone=> 10 20 30
You can use the read command to obtain the value from user input and assign a value to the variable.
Code:
echo -n "Enter \"var\""read varecho "The value of var is $var"<= 100=> The value of var is 100
5. Summary
We have learned some concepts about variables in Bash and syntax rules such as variable assignment and reference. I learned how to create a variable in a Bash script, assign values to the variable, and output the variable to the terminal through the echo command.