Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the basis of Shell variables

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

A brief introduction to variables

Variables are places and data markers where data are temporarily stored. The stored data exists in the memory space. The data corresponding to the variable can be extracted 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, the use of variables in programming is also necessary. Otherwise, it will be very difficult to finish the relevant work.

The way variables are assigned: write the variable name first, followed by "=", and finally the value. Spaces are not allowed before and after the "=" sign. In general, when defining a variable, you need to put double quotation marks on the value of the variable to prevent errors, especially if there is a space in the content of the value.

2. Types of variables

Variables can be divided into two main categories:

Environment variables, also known as global variables, can be used in the Shell that created them and any sub-shell derived from them. The environment can also be divided into custom environment variables and bash built-in environment variables; ordinary variables can also be called local variables and can only be used in the shell function or shell script that created them. III. Environmental variables

Environment variables generally refer to the convenience commands exported with "export" built-in commands, which are used to define the running environment of Shell and ensure the correct execution of Shell commands.

Environment variables can be set and created on the command line, but their values are also lost when the user exits the command line, so if you want to keep the environment permanently, it can be in the .bashrc file in the user's home directory, or in the global configuration / etc/profile or / etc/bashrc file. When you put the environment variables into the above file, they will be initialized each time the user logs in.

According to the system specification, the names of all environments are capitalized (not necessary, just customary). Before applying environment variables to user process programs, you should define them 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. Environment variables are usually defined and saved in the. bash_profile file or the global configuration file / etc/profile file in the user's home directory.

When you view a set variable, there are three commands that display the value of the variable:

① set: outputs all variables, including global and local variables

② env: show only global variables

③ declare: outputs all variables, functions, integers, and exported variables.

1) set environment variables

If you want to set an environment variable, use the "export" command after assigning a value to the variable or when setting the variable.

Syntax format:

① export variable name = value

② variable name = value

Export variable name

Of course, in addition to using the "export" command, you can also use the declare built-in command with the "- x" option to have the same effect.

Syntax format:

Declare-x variable name = value

If you want the method to make the environment variable permanent:

For users:

You can write the contents of variables in / root/.bashrc (recommended) and / root/.bash_profile files. For global purposes:

You can write the contents of variables in / etc/bashrc (recommended) and / etc/profile files.

If you want the script to be executed automatically after the system is powered on, simply put the script file name in the / etc/profile.d/ directory!

2) effective order of environmental variables

When you log in to a Linux system and start a bash shell, by default, bash looks for the settings of the environment in several files. These files can be collectively referred to as system environment files. The environment variable files checked by bash depend on how the system runs shell.

There are generally three ways for a system to run shell:

The shell that ① runs by default after the system user logs in.

② does not log in to run shell interactively

③ executes scripts to run non-interactive shell.

When the user logs in to the system. Shell starts as login shell, and the login shell loads environment variables in the order, as shown in the figure:

The / etc/profile (global environment variable file) is loaded first after ① users log in to the system. This is the default Shell master environment variable file on Linux systems. Any user who logs in to the system will load this environment variable file.

② the script file in the / etc/profile directory will not be executed until the / etc/profile.d file is loaded

③ then starts running $HOME/.bash_profile (user environment variable file); in this file, it looks for $HOME/.bashrc (user environment variable file); in this file, it looks for / etc/bashrc (global environment variable file).

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

4. Ordinary variables 1) define local variables

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

There are three ways to define:

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

The difference between these three definition variable files:

The first (without any quotation marks): it can be used when the content is a simple consecutive number, string, or path name. Without quotation marks, the value will be parsed when there are variables in the value; the second (single quotation marks): this definition is characterized by outputting what is in the single quotation marks when outputting the contents of the variables. regardless of whether there are variables and commands in the content (enclosed in reverse apostrophes). Will also output them as they are. It is more suitable for cases where pure strings are defined. The third (double quotation marks): the characteristic of this definition is that the variables and commands in the quotation marks are parsed before outputting the contents of the variables. Instead of the same output as the second one (single quotation marks). This approach is more suitable for variable definitions that come with variables and commands in a string (enclosed in reverse apostrophes) and want to parse them before outputting them.

As shown in the figure:

2) use the command as the value of a variable

There are two ways to use a command as a variable value:

① variable name = `Command `/ / enclose the command in reverse apostrophe ② variable name = $(command) / / recommended

Note that when a variable is followed by other characters, you must add "{}" to the variable.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report