如何编程设置在Linux中一个永久的环境变量

如何编程设置在Linux中一个永久的环境变量

本文介绍了如何编程设置在Linux中一个永久的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个小一些软件安装脚本。它所做的就是解压的目标焦油,然后我要永久设置一些环境变量 - 解包库的主要位置,并更新$ PATH。我需要以编程方式编辑的.bashrc 文件,添加适当的条目到底例如,或有另一种方式?什么是标准的做法?

I am writing a little install script for some software. All it does is unpack a target tar, and then i want to permanently set some environment variables - principally the location of the unpacked libs and updating $PATH. Do I need to programmatically edit the .bashrc file, adding the appropriate entries to the end for example, or is there another way? What's standard practice?

编辑:该软件包包括了一些运行脚本(20+)的,所有使用这些命名的环境变量,所以我需要设置它们以某种方式(变量名已被选定,使得碰撞是极不可能)

The package includes a number of run scripts (20+) that all use these named environment variables, so I need to set them somehow (the variable names have been chosen such that a collision is extremely unlikely)

推荐答案

兼容LSB(见spec)做法是创建在 /etc/profile.d中/ 文件夹中的shell脚本。

LSB-compliant (see spec) practice is to create a shell script in /etc/profile.d/ folder.

您的应用程序后,将其命名为(并确保该名称是唯一的),请确保该名称与 .SH 结束(你可能想为其他添加脚本贝壳以及)和导出您在脚本需要的变量。从该目录中的所有 * SH 脚本读取用户登录 - 在同一时间 / etc / profile文件 D。

Name it after your application (and make sure that the name is unique), make sure that the name ends with .sh (you might want to add scripts for other shells as well) and export the variables you need in the script. All *.sh scripts from that directory are read at user login--the same time /etc/profile is sourced.

请注意,这不是由庆典执行;相反,它是各种各样的协议。

Note that this is not enforced by bash; rather, it's an agreement of sorts.

这篇关于如何编程设置在Linux中一个永久的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:14