preface

This series of articles is just a syntax guide to the Bash shell, covering only variable definitions, loop statements, control statements… Do not include the introduction of shell commands.

In addition, about these basic grammar will not be a large and complete explanation, this is my years of learning experience, the content is too full, will be very impatient to see, in addition, only part of the things are often used, so only to the commonly used summary explanation, write too much, read also not too remember.

This article focuses on variables.

variable

The variable name

The characters of variable names can only be numbers, characters, and underscores. Does not contain Spaces, colons, # signs, and equals signs.

Variable names are case-sensitive.

Definition and assignment of variables

Common use of variables, definition does not need any keyword (except read-only variables such as the use of the keyword realonly), definition and assignment together (in fact, there is no variable definition in shell), such as: name=shell, define a variable, the value of the variable is shell

A read-only variable

With readonly definition, variable assignment cannot be modified as follows:

Variable scope

  1. Variables in shell scripts

Useful only in the current shell script, not in other scripts

  1. Shell terminal variable

After the current terminal is defined until the terminal is closed, the above read-only variable, open another terminal will not be used

  1. Global environment variable

All terminals and scripts are available, such as configured environment variables

Variable types

The same variable can be assigned to different types as follows:

 #! /bin/bash
 
 #string
 var="this is string"
 echo $var
  
 #integer
 var=123
 echo $var
  
 #floatVar = 2.1echo $var
Copy the code

Input of variable

Use the read command as follows:

#! /bin/bash
 
read name
echo "hello, $name"
Copy the code

Execute the result, enter the variable, assign it to name, and print it out:

For detailed use of READ, see the help documentation

Variable output

Use the echo command, which also has many parameters, to view the help documentation

Use of variables

If you want to refer to a variable, you can prefix the variable name with a symbol, as in the example above in the code or image, and define the variable name= CSDN, using the variable: symbol: The symbol, as shown in the code or image above, defines the variable name= CSDN, using the variable: Name can also be ${name}, if the variable is followed by another symbol in a string. Note also that the variable is referenced in the string. The use of single and double quotation marks in this character is explained later.

#! /bin/bash
welcome="hello"
# The variable has Spaces with the string that follows it
echo "I want to say $name world"
The # variable has no space after the string. If you don't use ${}, write $nameworld. The variable name is nameworld
echo "I want to say ${name}world"
Copy the code

The output result is shown as follows: