Introduction to Linux environment variables

Source: Internet
Author: User

The importance of a Linux environment variable concept

Originally used for Linux to learn the robot operating system (ROS), because have not been exposed to Linux, so do not understand some of the key concepts, resulting in a lot of detours, such as in the installation process of Ros, did not understand the installation tutorial 1.6 environment variables in the establishment of the directive meaning, The result is that sometimes a package created in your own workspace will not be found. Therefore, this paper, combined with some instructions in the Ros installation tutorial, focuses on the concept and application of Linux environment variables.

The concept of two environment variables

Environment variables are variables that describe the environment. An environment is a shell-related session and work environment where variables are variable quantities.

Classification and operation of three variables

Variables are divided into global variables and local variables;

Global variables are visible to child processes created by shell and Shell (inheritance);

Local variables are visible only to the shell that created them (temporary).

A local variable can become a global variable for use by the child process it creates (export);

Show all environment variables (PRINTENV);

Show all variables (set)

It is important to note that these own defined global variables and local variables are invalidated when you log out of bash.

Four PATH environment variables

The PATH environment variable determines where the command is located and the lookup order in the path variable;

Why is the lookup order in the path variable, because the order in which the commands are run is found in the following four ways:

1 executing commands with relative/absolute paths

2 The command is found by alias

3 bash built-in commands

4 order in the PATH variable

Five environment variable configuration file

The purpose of the system environment profile is to allow bash to read these files directly when it is launched, without our own definition.

The configuration file is divided into the entire system configuration file and the personal user profile.

Six different ways to start the shell

Unlike Shell launches, different configuration files are enabled.

There are three ways to start a shell:

1 Login Shell (default login Shell): Shell that needs to enter user account and password

2 Non-login shell (interactive shell): Shell that does not need to enter the account password

3 Running Scripts (non-interactive shell)

Login shell and non-login shell specifically how to invoke which configuration files, this article does not explain, we just need to know that the environment configuration file ~/.BASHRC can be called.

Seven examples: Setting up environment variables in the ROS installation tutorial

To install the official Ros tutorial, we will find that we have three choices when setting up environment variables:

The first: You can automatically add ROS environment variables every time you start a new shell

echo "Source/opt/ros/kinetic/setup.bash" >> ~/.bashrcsource ~/.BASHRC

The second type: Just add the ROS environment variable in your current shell

Source/opt/ros/kinetic/setup.bash

The third type: using zsh instead of bash

echo "Source/opt/ros/kinetic/setup.zsh" >> ~/.zshrcsource ~/.ZSHRC

Here we use the first two types of instructions.

We said that our own defined global variables and local variables are invalidated when you log out of bash, and you want to let your own defined variables not fail: Write a configuration file

Thus, there is the establishment of the first environment variable in Ros:

echo "Source/opt/ros/kinetic/setup.bash" >> ~/.bashrcsource ~/.BASHRC

The first line indicates that the string in the "" is written to ~/.BASHRC (need to be logged out and then logged in to take effect,>> indicates that the data flow output redirect "Append",> means "replace")

The second line indicates that the configuration file is read into the current shell. (Effective immediately)

So there's a second set of ROS environment variables.

Source/opt/ros/kinetic/setup.bash

Read the configuration file into the current shell.

So far, most of the concepts of environment variables have been introduced.

Introduction to Linux environment variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.