SHELL variable foundation detailed

1. Introduction to variables

Variables are places where data is temporarily stored and data tags. The stored data exists in the memory space, and the data corresponding to the variable can be retrieved by correctly calling the name of the variable in the memory space. The biggest advantage of using variables is to make program development more convenient. Of course, it is also necessary to use variables in programming. Otherwise, it will be difficult to complete the related work.

The way of variable assignment: write the variable name first, followed by “=”, and finally the value. No spaces are allowed before and after the “=” sign. Generally, when you define a variable, you need to put double quotation marks on the value of the variable to prevent errors, especially when there are spaces in the content of the value, you must put double quotation marks.

Two. Types of variables

Variables can be divided into two categories:

  • Environmental variables also They can be called global variables and can be used in the Shell that created them and any subshells derived from them. Environment commands can also be divided into custom environment variables and bash built-in environment variables;
  • Ordinary variables can also be called local variables, which can only be used in the shell function or Used in shell scripts.

3. Environment variables

Environment variables generally refer to the instructions exported with the “export” built-in command, which are used to define the operating environment of the Shell. Ensure the correct execution of Shell commands.

Environment variables can be set and created on the command line, but when the user exits the command line, these variable values ​​will also be lost. Therefore, if you want to save the environment permanently, you can set it in the user’s home directory. .bash_profile or .bashrc file, or global configuration /etc/profile or /etc/bashrc file. Put the environment variables in the above file, and these variables will be initialized every time the user logs in.

According to system specifications, all environment names are capitalized (not necessary, just habit). Before applying environment variables to user process programs, they should be defined everywhere with the “export” command.

Some common environment variables in the system, such as HOME, PATH, SHELL, UID, USER, etc., have been set by the /bin/login program before the user logs in. Usually environment variables are defined and saved in the .bash_profile file in the user’s home directory or the global configuration file /etc/profile file.

When viewing the set variable, there are three commands to display the value of the variable:
① set: output all variables, including global variables and local variables;
② env: only Display global variables;
③ declare: output all variables, functions, integers and exported variables.

1) Set environment variables

If you want to set environment variables, you must use the “export” command after assigning values ​​to the variables or when setting the variables.

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

Of course, except for using ” “export” command, you can also use the declare built-in command with the “-x” option to set the same effect.

Syntax:

  • declare -x variable name=value

If you want to make the environment The method for the permanent effect of the variable:

  • For users:
    The content of the variable can be written in the /root/.bashrc (recommended), /root/.bash_profile file;
  • For the global situation:
    You can write the variable content in the /etc/bashrc (recommended), /etc/profile file;

If you want to boot the system To execute the script automatically, just put the name of the script file in the /etc/profile.d/ directory!

2) The order in which environment variables take effect

When logging in to the Linux system and starting a bash shell, by default, bash will look for environment settings in several files. These files can be collectively referred to as system environment files. The condition of the environment variable file checked by bash depends on the way the system runs the shell.

There are generally three ways to run the shell in the system:
① The shell that runs by default after the system user logs in;
② The shell is run interactively without login;
③ The script is executed Run a non-interactive shell.

When the user logs in to the system. The shell will be started as a login shell. The order in which the login shell loads environment variables at this time is as shown in the figure:
Shell variable basics
①The user first loads /etc/profile (global environment variable file) after logging in to the system. This is the default Shell main environment variable file on Linux systems. Any user login in the system will load this environment variable file;
②After loading the /etc/profile file, the script file in the /etc/profile.d directory will be executed;
③After that, start running $HOME/ .bash_profile (user environment variable file); in this file, you will find $HOME/.bashrc (user environment variable file); in this file, you will find /etc/bashrc (global environment variable file).

If the user’s shell is not started when logging in (such as switching shell or ssh remote login), then this non-login shell will only load $HOME/.bashrc (user environment variable file) and go Find /etc/bashrc (global environment variable file). Therefore, if you want to supervise the environment variables and other contents set under the non-login shell, you need to write the variable settings in $HOME/.bashrc (user environment variable file) or /etc/bashrc (global environment variable file). Don’t write to $HOME/.bash_profile (user environment variable file) or /etc/profile (global environment variable file)! ! !

Four. Ordinary variables

1) Define local variables

Local variables can only be used during the lifetime of the user’s current shell!

There are three definition methods:

①Variable name=value
②Variable name='value'
③Variable name ="value"

The difference between these three definition variable files:

  • The first type (without any quotation marks): This can be used when the content is a simple continuous number, character string, or path name. Without quotation marks, when there are variables in the value, it will be parsed and output;
  • The second type (single quotation mark): The characteristic of this definition method is: what is in the single quotation mark when outputting the content of the variable What to output, regardless of whether there are variables and commands in the content (enclosed with reverse apostrophes). They will also be output as they are. It is more suitable to define a pure string.
  • The third type (double quotation marks): The characteristic of this definition method is that the variables and commands in the quotation marks are parsed before outputting the content when outputting the content of the variable. Instead of outputting as-is with the second type (single quotes). This method is more suitable for variable definitions that have variables and commands in the string (enclosed with reverse apostrophes) and want to parse them before outputting them.

2) Use commands as variable values

There are two ways to use commands as variable values: p>

①Variable name=`Command`
//Enclose the command with back apostrophes
②Variable name=$(command)
//Recommended use

Note that when there are other characters after a variable, “{}” must be added to the variable.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5516 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.