问题描述
我正在为一些软件编写一个安装脚本。它所做的只是解压缩目标tar,然后我想永久设置一些环境变量 - 主要是解压缩的lib的位置并更新$ PATH。我需要以编程方式编辑 .bashrc
文件,添加适当的条目到底,例如,还是有另一种方式?什么是标准做法? 编辑:该软件包包含大量运行脚本(20+),所有这些都使用这些命名的环境变量,所以我需要以某种方式设置它们(已经选择了变量名称,使得冲突是非常不可能的)
LSB兼容)实践是在 /etc/profile.d /
文件夹中创建一个shell脚本。
在应用程序之后命名它(并使确保名称是唯一的),请确保名称以 .sh
(您可能还想为其他shell添加脚本)以及导出
脚本中需要的变量。所有 *。sh
从目录中读取的脚本将在用户登录时读取 - 同时 / etc / profile
是 source
d。
请注意,这不是由 bash
;相反,这是一种协议。
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?
Edit: 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-compliant (see spec) practice is to create a shell script in /etc/profile.d/
folder.
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 source
d.
Note that this is not enforced by bash
; rather, it's an agreement of sorts.
这篇关于如何在Linux中编程设置永久环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!