Shell variable basis Detailed

I. Introduction variable

Local variables and data flag is for temporarily storing data, the data stored in memory space, may be removed with the data corresponding to the variable name by correctly calling memory space variables. Using Variables The greatest benefit is to make application development easier, of course, you must also use a variable in programming. Otherwise it is difficult to complete the related work.

Variable assignment mode: the first to write a variable name is followed by "=", and finally the value. "=" Spaces are not allowed before and after the number. Under normal circumstances, the definition of variables, the values of variables needed to double quotes, in order to prevent errors, especially when the value of the contents of a space, must quotation marks.

Second, the type of the variable

Variables can be divided into two categories:

  • Environment variables can also be called a global variable, you can create them Shell and any sub-shell derived in use. It has made the environment can be divided into custom environment variables and bash built-in environment variables;
  • Ordinary variables can also be called local variables can only be created in their shell function or shell script to use.

Third, the environment variable

Environment variable is usually refers to "export" to export built-in command will make for defining Shell operating environment, ensure the correct implementation of Shell commands.

Environment variables can be set at the command line and create, but the user exits the command line, the value of these variables will also be lost, so if you want to permanently preserve the environment so that they can .bash_profile or .bashrc file in the user's home directory or global configuration / profile or / etc / bashrc file / etc. In speaking environment variables into the above file, each time a user logs of these variables are initialized.

The system specifications, the name of the environment are all uppercase (not necessary, just get used to it). Before the user environment variables used in the process of the program, should be defined everywhere with "export" command.

Some common system environment variables, such as: HOME, PATH, SHELL, UID, USER, etc., before the user logs had been / bin / login program set up. Usually environment variable is defined and stored in the user's home directory or file .bash_profile global configuration file / etc / profile file.

When viewing a set of variables, there are three commands to display the values of variables: set, env and declare.
① set: all output variables, including global and local variables;
② the env: display only global variables;
③ DECLARE: all output variables, functions, and variables have been derived integers.

Display all the parameters set-o bash Shell configuration information.

1) set the environment variable

If you want to set an environment variable, it is necessary or use the "export" command when setting variable after variable assignment.

Syntax:
①export value variable name =
② value variable name =
Export variable name

Of course, except that "export" command can also be used with a built-in command declare "-x" option can also be set to the same effect.

Syntax:

  • declare -x variable name = value

If you want to make permanent the environment variable method:

  • For users:
    variable content can be written in /root/.bashrc (recommended), / root / .bash_profile file can be;
  • For Global:
    variable content can be written in the / etc / bashrc (recommended), / etc / profile file can be;

If you want to automatically execute the script after the system is turned on, just the name of the script file directory can be placed under /etc/profile.d/!

2) environmental variables order

Linux system when you log in and start a bash shell, by default, bash looks set environment in a number of documents. These files can be collectively referred to as the file system environment. Check the situation bash environment variables file depends on the system running the shell mode.

Run shell systems are generally three ways:
① through the system the user's default login shell to run;
② run non-interactive login shell;
③ execute a script to run non-interactive shell.

When users log into the system. The shell as a login shell is started, then the login shell loading sequence environment variables as:
Shell variable basis Detailed
① after login first load / etc / profile (global environment variable file). This is the default on Linux systems Shell environment variables master file. Any user login system will load the environment variable file;
② When finished loading / etc after / profile file will execute the script file in the directory /etc/profile.d;
runs $ HOME / .bash_profile (user environment variables after ③ file); in this paper, will go to $ HOME / .bashrc (user environment variables file); in this paper, will go to / etc / bashrc (global environment variable file).

If the user's shell startup (such as switching shell or ssh remote login) when not logged in, this non-login shell will only load $ HOME / .bashrc (user environment variables file), and will go to / etc / bashrc (Global environment variable file). So if you want a non-login shell can also supervise the contents of environment variables set, you need to write a variable set in $ HOME / .bashrc (user environment variables file) or / etc / bashrc (global environment variable file). Do not write to $ HOME / .bash_profile (user environment variables file) or / etc / profile (global environment variable file)! ! !

Fourth, the common variable

1) define local variables

Local variables can only be used in the user's current shell lifetime!

The method defined in three ways:

 ①变量名=value
 ②变量名='value'
 ③变量名="value"

The difference between these three definitions of variables file:

  • The first (without any marks): When the content of simple sequential number, string, pathname, can be used as such. Without quotation marks, will be parsed in the output value, there are variable;
  • The second (single quotes): the definition of the characteristics of this approach is: if the output variable content in single quotes is what on what output, regardless of whether or not there are variables and command content (required to turn the anti-apostrophe enclosed). We will put them is output. More suitable for the case of pure defined strings.
  • The third (double quote): This definition is characteristic: when the contents of variables and output variables of the parsed command quotes then output. And instead of a second (single quotes) is output as the same. This approach is more suitable and command variables included (trans required turn enclosed apostrophe) and want to define the variable and then output to parse string.

2) as a command variable value

The command as a variable value in two ways:

①变量名=`命令`
//用反撇号将命令括起来
②变量名=$(命令)
//推荐使用

Note that when a character and there are other variables, which must be enclosed in '{}. "

Guess you like

Origin blog.51cto.com/14157628/2435592