Meaning variables
When we use the Linux command or script, there will be some output, then the information of these outputs can be 变量
temporarily stored, to prepare for our next command or script to use.
The definition of variables
Definitions of the variables suggested a meaningful English words to represent variables, do not use a simple letter a
, b
variable names defined this kind. Shell scripts written because we actually want to be looked at, so we defined a variable, the variable is to have specific meaning, read Shell script to make it easy for people to understand.
Variable naming rules:
- Letters, numbers, underscores
- Does not begin with a number
Assignment of variables
As a process variable assignment, called variable substitution
Shell script variables are not case-variable type
Rule variable assignment: variable name = variable value
var=123
Note: The left side of the equal sign to the right of the assignment can not have spaces, so if there is a space to perform when an error is reported back
[root@lincoding ~]# var=123
[root@lincoding ~]#
[root@lincoding ~]# var =123
-bash: var: command not found
[root@lincoding ~]# var = 123
-bash: var: command not found
Because space appears, Shell would think that in front of the command is not the variable
Assigning variables using let
[root@lincoding ~]# let num=10+10
[root@lincoding ~]# echo $num
20
Use let
can do simple number crunching
The commands assigned to the variable
[root@lincoding ~]# ls_cmd=ls
[root@lincoding ~]# ${ls_cmd}
anaconda-ks.cfg install.log install.log.syslog
You can also use the command assigned to a variable, when using a variable, equivalent to execute the command
The command assigns the result to a variable, use $ () or ``
[root@lincoding ~]# file_list=$(ls /tmp)
[root@lincoding ~]# echo ${file_list}
pear yum.log
[root@lincoding ~]# file_list=`ls /tmp`
[root@lincoding ~]# echo ${file_list}
pear yum.log
The benefits of using this approach is to avoid repeat the same orders to increase the cost of a server, but the command is executed only once, and save the results to the command variable, do the next use
Variable values are special characters like space can be enclosed in double quotation marks "" and single quotation marks ''
[root@lincoding ~]# var="1 2 3"
[root@lincoding ~]# echo $var
1 2 3
[root@lincoding ~]# var='a b c'
[root@lincoding ~]# echo $var
a b c
When the string needs double quotes, single quotes need to define a string
[root@lincoding ~]# var='"a" "b" "c"'
[root@lincoding ~]# echo $var
"a" "b" "c"
When the string needs single quotes, with double quotes to define a string
[root@lincoding ~]# var="'a' 'b' 'c'"
[root@lincoding ~]# echo $var
'a' 'b' 'c'
There is a difference between double quotation marks and single quotation marks, is the single quotes in the string, whether special characters are interpreted as a normal string
[root@lincoding ~]# string="${var} , Shell"
[root@lincoding ~]# echo $string
hello , Shell
[root@lincoding ~]# string='${var} , Shell'
[root@lincoding ~]# echo $string
${var} , Shell
summary
Shell is defined in the variable is not necessary to define the type, time variable assignment equal on both sides No spaces, variables can be assigned to Linux commands, you can also assign content to run Linux command output results returned, you can also do simple numerical calculation and double and single quotes defined string is a certain difference, single quotes defined strings will be interpreted as a normal string, regardless of whether there are special characters.