02 Shell Variables

What is Shell variable
  mathematical equations in principle there is, the equation is to use a name (unknown) instead of the unknown is operated. Similarly, in the Shell, the variable is replaced by the name of a thing.
  Definitions: Shell in the variable is a character or string.
  The role of variables: variable is a name used in place of a complex content. The content can be anything, such as other variables, path, value, function, expression and so on. Use variables can greatly simplify the code written, we can even operate directly on the variable without knowing what the variable represents.

Define variables
  only way to define (generate) variable is assigned. Assignment is connected with an equal sign and variable content, variable equal sign written on the left, the right content, the "variable = content" format. Note equal on both sides No spaces, contents of the variable to add double quotes.

Classification and characteristics of the variables
  Shell variables fall into two categories: environmental variables (global variables) and general variables (local variables).
  Environment variables can be used in the definition of their child Shell and Shell. bash built a number of environment variables, you can also customize the environment variables.
  Ordinary variables can only be used in the definition of their function or Shell Shell script, generally defined by the developer in the development of the script.

Environment Variables
  Environment Variables generally refers to the variable derived using export command, used to define the Shell operating environment, ensure the normal execution Shell command. Shell to determine the user's login name by environment variables, command path, terminal type, login catalog.
  The name of the environment variable specification in all caps. And use the export command to export the definition of the previously defined environment variables.
  Custom environment variables in three ways

export the contents of a variable name =
variable name = content; export variable names
declare -x variable name = content
  saved the home directory of the system environment variables in .bash_profile or / etc / profile in. Modify environment variables as long as redefined on the line. If you want permanent environmental variables, environmental variables into the above system configuration file.
  View variables set, env, declare command. Show all set environment variables and ordinary. env only display all the environment variables, DECLARE all output variables, functions, integer.
  Output environment variable $ variable name. (Echo output to double quotes, but there are $ variable output without the double quotes). such as

echo $ variable name
printf "$ variable name"
  without quotes and double quotes output the same effect.
  Delete environment variables unset command. Format: unset variable name.

Ordinary variables
  common variables are only valid for the lifetime of the current Shell, Shell shut down a new process or the normal variable disappears.
  There are three common variable definitions written.

Variable name = content
variable name = 'content'
variable name = "content"
  in quotation marks problem is the focus of Shell programming! Without quotation marks, apostrophes, meaning double quotation marks are different. When the digital content is a continuous string, the path is no quotation marks may be used, without special characters escaped quotes variables when the variable is parsed. Not escaped single quotes, quotes on what is what the output is a string. No double quotes and quotes the same role, will escape double quotation marks, for intermittent output content.
  There is not only variable is defined when the quotes of the points, there are also quotes the output of the points, the role of the quotes above.
  Quotes specifications: number can not fight quotes, want to parse the output after unification fight double quotation marks, as they want to play the output of a single quote.
  Common variable output is also variable name $ or $ {name} variable, two way no difference, the same effect.

  If you want the results of the command assigned to a variable, the format is:

`` Command variable name =
variable name = $ (command)
1
2
Variable name specification defined
  variable names can only numbers, letters, underscores (_) composition, can not start with a number, which can not have spaces.

Guess you like

Origin www.cnblogs.com/Nonstopcoding/p/11183106.html