Linux Shell Script Series Tutorial (iv): Adding environment variables using functions _linux shell

Source: Internet
Author: User
Tags eval

First, Introduction

Environment variables are typically used to store path lists that are used to search for executables, library files, and so on. For example: $PATH, $LD _library_path, they usually look like this:

Copy Code code as follows:

Path=/usr/bin;bin
Ld_library_path=/usr/lib;lib

This means that whenever the shell needs to run a binary executable, it looks for/usr/bin first and then looks for/bin. In ubuntu14.04, the path and Ld_library_path store paths are as follows:

Copy Code code as follows:

Path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
/sbin:/bin:/usr/games:/usr/local/games
Ld_library_path= "" #默认情况下, the variable is undefined

Second, add environment variables

When you have to compile the generator using source code and install it into a specific path, an extremely common task is to add the bin directory of the program to the PATH environment variable and add the library directory on which it relies to join the LD_LIBRARY_PATH environment variable so that the software can be run correctly from the command line terminal. Suppose we install TestApp into the/opt/testapp directory, its binaries are in the bin directory, and the library file is in the Lib directory, the method for adding environment variables is as follows:

Copy Code code as follows:

Export Path=/opt/testapp/bin: $PATH #路径之间以冒号 (:) separated
Export Ld_library_path=/opt/testapp/lib: $LD _library_path

Iii. adding environment variables using functions

When there are fewer paths to add, you can manually add them one at a time, but when the path is longer, manual addition becomes time-consuming and the likelihood of error increases. In fact, you don't have to do this, we can add a function of the environment variable in. bashrc-, the function completes the addition of the environment variable, for example, we can complete the task of section 1.4.2 with the following function:

Copy Code code as follows:

Prepend () {[d ' $] && eval $1=\ ' $ ': ' \$$1\ ' && export $}

This function requires two parameters, 1 and 2, where 1 is used to store the name of the environment variable, such as path,2 for the environment variable to be added, such as/opt/testapp/bin.
The function first executes the [-D "$"] statement. Determines whether the path to be added exists, executes the following statement, or ends the function, and then executes the second statement that adds the path to the beginning of the corresponding environment variable, adding the path using the forward interpolation method; 1 makes the environment variable effective.

Note: statement eval $1=\ "$": ' \$$1\ ' is more difficult to understand, $$1 can be written as \${\$1}, which is easier to understand, means first take the first parameter of the function, and then extract the contents of the parameter. For example, the environment variables in section 1.4.2 can be added in the following ways:

Copy Code code as follows:

Prepend () Path/opt/testapp/bin #$1 for path,$2/opt/testapp/bin
Prepend () ld_library_path/opt/testapp/lib #$1 for ld_library_path,$2/opt/testapp/lib

Adding an environment variable with the above function is really convenient, but the function itself is not perfect, when the environment variable is empty, this will add an extra colon at the end (:), you need to make the following changes to make the function more rigorous:

Copy Code code as follows:

Prepend () {[d ' $] && eval $1=\ ' \$2\$\{$1:+ ': ' \$$1}\ ' && export $}

The change is only a second statement, where a shell parameter extension is used:
Copy Code code as follows:

${parameter:+expression}

Use expression values when and only if the parameter has a value and is not empty, so you can avoid adding null variables.

Related Article

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.