Linux Shell script series tutorial (4): Add environment variables using functions

Source: Internet
Author: User

Linux Shell script series tutorial (4): Add environment variables using functions

This article mainly introduces the Linux Shell script series (4): Adding environment variables using functions. This article introduces the knowledge of environment variables, the following describes how to add environment variables and use functions to add environment variables. For more information, see

I. Introduction

Environment variables are usually used to store the path list. These paths are used to search for executable files and library files. For example, $ PATH and $ LD_LIBRARY_PATH usually look like this:

The Code is as follows:

PATH =/usr/bin; bin

LD_LIBRARY_PATH =/usr/lib; lib

This means that when the shell needs to run a binary executable file, it will first find/usr/bin and then find/bin. In ubuntu14.04, PATH and LD_LIBRARY_PATH are stored as follows:

The Code is as follows:

PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:

/Sbin:/bin:/usr/games:/usr/local/games

LD_LIBRARY_PATH = "" # by default, this variable is not defined.

2. Add Environment Variables

When you must compile a program using source code and install it in a specific PATH, an extremely common task is to add the bin directory of the program to the PATH environment variable, add the library directory it depends on to the LD_LIBRARY_PATH environment variable so that the software can be correctly run from the command line terminal. Suppose we install testApp to the/opt/testApp directory, and its binary file is in the bin directory, and the library file is in the lib directory, the method for adding environment variables is as follows:

The Code is as follows:

Export PATH =/opt/testApp/bin: $ PATH # separate paths with colons (:)

Export LD_LIBRARY_PATH =/opt/testApp/lib: $ LD_LIBRARY_PATH

3. Add environment variables using functions

When the number of paths to be added is small, you can manually add them one by one. However, when the number of paths is large, manual addition will consume time and increase the possibility of errors. In fact, we don't have to do this. add a function that can add environment variables to bashrc. The function can add environment variables. For example, we can use the following function to complete the tasks in section 1.4.2:

The Code is as follows:

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

This function requires two parameters: 1 and 2. 1 is used to store the name of the environment variable, such as PATH and 2 are used to store the environment variable to be added, such as/opt/testApp/bin.

The function first runs the [-d "$2"] statement to determine whether the path to be added exists. If yes, run the following statement. Otherwise, the function ends. Then, run the second statement, this statement adds the path to be added to the beginning of the corresponding environment variable, that is, add the path using the forward insertion method. Finally, use export $1 to make the environment variable take effect.

Note: The statement eval $1 = \ "$ 2': '\ $1 \" is hard to understand. $1 can be written as \$ {\$ 1 }, this is easy to understand, indicating that the first parameter of the function is obtained first, and then the content of this parameter is extracted. For example, you can add environment variables in section 1.4.2 as follows:

The Code is as follows:

Prepend () PATH/opt/testApp/bin #$1 is PATH, $2 is/opt/testApp/bin

Prepend () LD_LIBRARY_PATH/opt/testApp/lib #$1 is LD_LIBRARY_PATH, $2 is/opt/testApp/lib

Adding environment variables using the above functions is indeed quite convenient, but this function is not perfect. When the environment variable is empty, it will add an extra colon (:), make the following changes to make the function more rigorous:

The Code is as follows:

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

Only the second statement is changed. Here, a shell parameter extension form is used:

The Code is as follows:

$ {Parameter: + expression}

The expression value is used only when parameter has a value and is not empty. This avoids adding null 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.