Birds Beginner Shell Programming (seven) variable reference and scope

Variable reference

So defined the variables, how to print the value of the variable it? Variables way of reference.

  • ${变量名}Referred to as a reference to the variable
  • echo ${变量名}View a variable value
  • ${变量名}It may in some cases be omitted as $变量名
[root@lincoding ~]# string="hello Shell"
[root@lincoding ~]# echo ${string}
hello Shell
[root@lincoding ~]# echo $string
hello Shell

Then there are the variables enclosed in braces and did not spend any difference in brackets is it?

[root@lincoding ~]# echo $string9

[root@lincoding ~]# echo ${string}9
hello Shell9

Can be found in the references stringafter added a variable 9, not a reference to add braces, will string9as a variable name, there are references plus braces, then print stringthe variable tails and add a9


The default scope variables

We will only take effect in the current Shell environment by defining variables, when switching to another Shell, the variable is not defined before the entry into force of

We define a variable in Shell scriptstr

#!/bin/bash

str="my shell"
echo ${str}

Shell script execution time, it will print the value of the variable Shell script definition. The current terminal reference variables Shell script prints the null value.

[root@lincoding ~]# ./test.sh
my shell
[root@lincoding ~]# echo ${str}

[root@lincoding ~]#

Explanatory variable strscope only in the Shell script.

If you define a variable in a terminal, Shell script to reference the variable will take effect you?

[root@lincoding ~]# mystr="abc"
[root@lincoding ~]# cat test.sh
#!/bin/bash

echo ${mystr}
[root@lincoding ~]# ./test.sh

[root@lincoding ~]# bash test.sh

[root@lincoding ~]# . test.sh
abc
[root@lincoding ~]# source test.sh
abc

Were used to perform the above four ways to run the script, the impact of implementation of the four previous section also explained in detail.

Mode 1 and Mode 2 will produce a child process to execute the script, due to the variable scope of the current terminal is defined only in the current terminal, so the child process reference variables defined in the parent process is not in force.

Three ways and means of four is not going to produce a child process, but directly within the scope of the current terminal environment in the implementation of the script, so are variable, so the reference to the variable is in effect.


export Export variables

Suppose you want the parent process defined variables also come into effect in the child or the child Shell, then the need to use exportthe variable export, specific ways to use the following example:

[root@lincoding ~]# mystr="abc"
[root@lincoding ~]# bash test.sh

[root@lincoding ~]# export mystr
[root@lincoding ~]# bash test.sh
abc
[root@lincoding ~]# ./test.sh
abc

Visible in use export, the terminal defined variables, test.shscript references the variable is in effect. Also said that the child can get the value of a variable defined in the parent process.

If the variable is used up, trying to clear the variable, you can useunset

[root@lincoding ~]# unset mystr
[root@lincoding ~]# echo ${mystr}

[root@lincoding ~]#

summary

The default variable scope is Shell's own, if you want a variable for the child to use Shell or the child process parent process, we need export 变量名to export variables keyword, if you no longer use the variable, in time to use the unset 变量名value of the variable empty.

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11615691.html