shell variables study notes 2--

Defining and using variables

Scripting languages generally do not need to specify the type when you define a variable, you can direct assignment, Shell  variables can also follow this rule.

If the shell does not declare a data type, default string type

In the Bash shell, by default, the value of each variable are strings , there is no use quotation marks Whether you assign values to variables, values are stored as a string. Unless you declare the variable type declaration

Without the dollar sign when you define a variable (or without the dollar sign when assign values ​​to variables)

To add a dollar sign when using variables

Variable name outside the curly braces are optional, plus without anything, add braces to help identify the boundary interpreter variables. Recommended for all variables braces { }, this is a good programming practice.

Defined variables, can be re-defined, re-defined do not add a dollar sign (for variable assignment is to define a variable?)

When variable assignment, the variable name and the equal sign and a value between no spaces

 

= your_name " tom " // define variables without dollar signs, can not have a space between the variable name and the equal sign 
echo $ your_name // use the variable 
your_name = " alibaba " // redefine the dollar symbol is not added 
echo $ your_name
the your_name field = "qinjx"  outer echo $ your_name // variable name without {}  echo $ { the your_name field } // {} plus the variable name, recommended
for skill in Ada Coffe Action the Java ; do echo "the I AM AT $ {skill} Good Script" //      border plus a variable name} {effectively identify variables
 done

Variable naming

  • Named only letters, numbers and underscores, the first character can not start with a number.
  • Bash can not be used in keywords (available bash --help command to see the reserved keyword)

Shell valid variable name example

RUNOOB
LD_LIBRARY_PATH
_var
var2

Invalid variable naming

? Var = 123 // can not use the characters other than letters, numbers and underscores
user*name=runoob
= Runoob 1name // begin with a number can not be used

 The value of the variable

If the value of the variable value does not include any whitespace (e.g. spaces, Tab indent, etc.), the marks may not be used;

If the value contains a blank character, then you must use quotation marks around it.

Use single quotes and double quotes are also differentiated

A single quote ' 'when the value of the variable surrounded by single quotes is what it is what the output , even with variable content and command ( command requires trans caused to ) them as they will be output. This embodiment is suitable for a display defined in the case of pure string variable that is parsed undesirable, the scene commands.

Double quotes " "when surrounded by the value of the variable, which will first resolve variables and output commands , rather than double quotation marks in the variable name and the command is output. This approach is more suitable strings accompanied by variable and command and you want to define and then parse the output variables.

My advice: If the content of the variable is digital, you can not add quotation marks; if you really need is output as single quotation marks; no other special requirements such as strings are the best in quotes, double quotes when defining variables is the most common usage scenarios.

 

The results of the command assigned to the variable

Shell will also support the implementation of the results of the command assigned to a variable, common are the following two ways:

variable=`command`
variable=$(command)

 

The first approach marks the anti command ` `(Esc key located below) surrounded, trans and single quotes are very similar, create confusion, it is not recommended in this way;

The second way to use the command $()surrounded distinguish more clearly, it is recommended to use this way.

 

Read-only variables

Use readonly command to define variables as read-only variables, the value of read only variables can not be changed.

#! / bin / bash 
myurl = " http://www.google.com " 
Readonly myurl after // can not be changed, otherwise it will error

 

Delete variables

unset variable_name

 

shell variable scope

Shell scoped variables can be divided into three

Global variables : current variables (variables defined outside the function) defined in the shell, the internal variables defined function (without pre variable local) are global variables, global variables are only valid in the current shell, the shell comprising an internal function of the current, invalid sub-shell and the shell father

Local variables : internal function definition, and preceded by a local variable that is only valid inside the function

     Internal variables defined function, when there is no local to global variables, local variables when local

Environment Variables : valid in current shell and sons shell. We can use the export will become environment variables defined variables.

Personal understanding: when the parent shell to produce sub-shell, the father of the child shell will be the shell environment variables as their own copy of environment variables. The global and local variables are not copied. So sub-shell appears to be able to access the parent shell environment variables, in fact, a visit to your own environment variables (ie, replicas of the parent shell environment variables), after sub-shell generation, environment variables and parent shell sub-shell environment variables They are independent and do not affect each other after modification

 

 

Environment variables are also temporary

A common global variables by export command after export into a variable environment. But export export environment variables are temporary and only valid in the current shell and its sub-shell. When re-open a new shell and does not work.

To get a variable effective in all Shell processes, regardless of whether there is a parent-child relationship between them, how to do it?

Shell will write only variable configuration file in order to achieve this goal! When Shell process will be executed each time you start the configuration file to do some initialization code, if the variable in a configuration file, so each time you start the process will define this variable.

 

Guess you like

Origin www.cnblogs.com/gaoBlog/p/10278518.html