如何以编程方式在

如何以编程方式在

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

问题描述

我正在为一些软件编写一个小安装脚本.它所做的只是解压目标 tar,然后我想永久设置一些环境变量——主要是解压库的位置并更新 $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 结尾(您可能还想为其他 shell 添加脚本)和 export 脚本中需要的变量.该目录中的所有 *.sh 脚本都会在用户登录时读取——同时 /etc/profilesourced.

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.

请注意,bash 不强制执行此操作;相反,这是一种协议.

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

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

08-01 06:18