Linux environment variables: description, features

Linux environment variables are a special kind of variable defined by the command line, system, or shell. Their use is possible with almost any software during the implementation of functions. Especially often they are used in the implementation of functions of low and hardware levels. For example, the PWN command, while working on the command line, uses one of the system environment variables to save the previous path to the file being processed.

Types of environment variables

Environment variables displayed in the system




According to the declaration method and initialization methods, Linux environment variables can be of three different types:

  1. The local type of variables and constants is initialized during system startup and, after exit, is deleted by the built-in deconstructor. Deletion of this type of data will occur in any case, regardless of how the connection to the hardware is made: through remote access devices, emulating a terminal, or creating a session through a virtual machine. During processing, they are not stored in any of the files or directories. Creation and deconstruction of variables occurs due to specialized commands in the control shell.
  2. Linux user environment variables are initialized for each user of the operating system. Detection and loading occurs during login to the operating system. As with other species, regardless of the entry through the local terminal or remote access, the determination is automatic. Saving data is provided in documents that are responsible for the configuration of the device. It is also possible to store them in other documents located in user folders. Most often found in bash_profile, bash_login and other configuration files.
  3. Systemic This view is available during the operation of the system to each user. They are used to control system functions and are required for the correct operation of the operating system. Download is carried out from the directory with the system environment files, profile, etc / environment and many others.
  1. Work with data




Files in which variables are stored

During operation of the Linux operating system both during remote start-up and in the mode of the local terminal, processing and change of variables occurs. Some of them need to be saved for future use. Others are initialized automatically using some tools. To change them and control some functions, you will need to edit the data files in which their values ​​are stored. These files are located in user and system directories. Below are the main files and the data stored in them.





BASHSRC Document

Almost all user data is stored in this document. A separate file is created for each user. Download is made during the creation of a terminal type session.

Linux environment variables




Variables start with a new session. Thus, user settings, data, and, if necessary, applications from the previous terminal launch are loaded.

BaSH_Profile File

The data from this file is required for remote connection of a new user through a secure shell through a network protocol operating at the application level and through the tunneling of transmission control protocols. Such a file can be deleted by the system or by the user. In this case, the data can be placed in the Bash_login or profile files. When you delete them, it is impossible to correctly control the system. Suppose the start of a data recovery function.

Environment File

The Environment document, which is located in the ETC system directory, is a repository of information for Linux environment variables that are managed at the operating system level. The extraction and operation is carried out at each start in any of the modes (through the local terminal or with remote access to the operating system using the SSH protocol (Secure Shell). The data is available for each user who has entered the operating system shell. This file is executed every time you start OS by all users.





Bash Processing




User system files

In addition to editable by the user, there are also system files into which data is written. Storage is performed in the ETC system directories. If necessary, it is possible to edit them by adding certain parameters.

User file to log in through the local terminal

The document, which is located in the / etc / bash.bashrc directory, is necessary for the user at work. The functioning of this file and its environment variables is possible only when starting applications through the local terminal. When you try to access documents through the secure shell protocol, errors may appear about insufficient rights.

Command Shell Types




User Profile for SSH Access

In the Profile file, which is located in the ETC directory, user variables are located. A file can only be executed at startup through remote access. This loads user settings and other data. However, the variables from ETC / Profile are not available for users who gained access to the operating system through the local terminal, that is, during normal startup.

Basic environment variables

When working on Linux, the environment is made up of many different variables. These include the type of access, the directory of the home area, the name of the program that performs the interpretation, executable files and programs, open directories and additional functions. All these parameters are called the environment and determine the operation of the device. Data is stored in Linux environment variables. The built-in command line of the operating system allows you to manage these values ​​directly, which provides deeper access to files and processes.

Linux shell




In the advanced Bash command shell, it is permissible to use shell parameters for this purpose. After certain changes, it is possible to export these variables to the Linux environment. Thus, additional process control and the creation of new ones are carried out. This allows you to add new data to the processes running through the interpreter. This applies to both the command control tool and other interpreters that can be launched from the main one.

Linux environment commands have their own unique identifiers. The same goes for bash shell options. In order to access these parameters, you will need to add a reference type to the identifier. This is done through the $ sign.

Identifier

Executable value, function

USER

This variable contains the name of the user who logged into the operating system.

UID

The numeric value that the user logged into the operating system receives.

Home

The directory in which the current user data is stored.

Pwd

The path to the files currently being processed.

SHELL

The identifier of the data processing software (shell).

$

Data about the process launched by the shell.

PPID

ID of the parent process that started the current one.

?

An error or termination code of the last command shell executed.

In addition, there is an environment variable path linux, which is responsible for the path to the executable file. In general, the table above shows almost all the main parameters of the shell.

How to add environment variables on Linux

This can be done independently when working with the operating system and the bash shell. To add a new parameter to the environment, you will need to use a certain set of commands.

To set the Linux environment variable, type var = the parameter you want to enter into the var shell command shell. Then, you will need to export this parameter using the export var = parameter command. To access the link you need to add a dollar symbol to the line. Therefore, the variable is created only for the current session. Moreover, this is possible only at the entrance to the shell through the local terminal. In the remote version, access in this way will not work. Viewing Linux environment variables is done through standard commands. It is important to note that this is completely independent of the login method.

To view the Linux environment variables, you also need to use the export command. An alternative is the env command, which displays the parameter in a cleaner form. How to output Linux environment variables? For a complete list, you can use the Set command.

Work through the terminal




Removing environment variables

There are several ways to deconstruct such a variable from a Linux environment. Each method has its own characteristics and characteristics.

The ENV command is normally used to set shell variables. However, adding -i will delete the parameters locally. Thus, the team is carried out without involving variables. The full command is as follows:

env –i [Var = Parameter] The action to be performed.

Moreover, instead of Var, you can set any value of the environment variable. For example, $ env -i bash will launch a shell without any parameters. While working with the shell, it is impossible to change or initialize the values, but during subsequent launches without the -i parameter it is possible to work with all built-in functions.

Unset command dereferences a command or parameter. The same goes for environment variables. They are completely removed from the programmable shell managed by the shell until the end of the current session. The command syntax looks like this:

$ unset Variable_name.

In addition, to remove a parameter, it is allowed on Linux to set the environment variable to a null value. Thus, the parameter is deleted until the end of the current session. Setting an empty variable works for any type of connection.

Linux process environment variables

The process of creating or initializing variables in the shell is done using built-in functions. When creating parameters in this way, they are saved for all subsequent system starts. While most versions and distributions of the Linux operating system recommend changing or adding new variables to the ETC / PROFILE directory. It is worth considering that most of the processes can control with this type of parameters and change their value. Any scenario can be used to change values. However, this is prohibited by Unix conventions.

To set system parameters, you need to edit or touch the following files:

  • / etc / profile - when working with this document, it is realistic to set the values ​​of environment variables for command shells;
  • / etc /bash.bashrc - used to create and assign variables to the interactive shell, and also necessary for bash scripts.
  • / etc / environment is needed for the env authentication plug-in to work.

On Linux, setting environment variables of a system type is carried out only in these files.

You can declare and initiate environment variables in upper and lower case. However, it is considered more correct to create them only in the upper, that is, in capital letters. The benefit of them is the ability for each specific user to execute software code in different ways. Thus, the utilities will be launched with the selected settings depending on the environment variable UID or USER. Information can also be obtained from the HOME catalog.

Operating systems with different types of access and users require a different set of parameters. To view Linux environment variables for users, you may need to use the console shell and Env or Set commands. In this case, a list of current parameters will be displayed. If necessary, use the Source command and the name of the file to which the new environment variable has been added.




All Articles