找出 bash 中最后设置环境变量的位置

发布于 2024-10-22 03:46:59 字数 441 浏览 1 评论 0原文

好吧,我知道有一个 bash 调试器。但我正在寻找的是,如果我的启动脚本之一中有一个环境变量,并且我不知道它是如何设置的或它可能在哪里,除了详尽地搜索脚本之外,还有其他方法可以找到它吗?

我的意思是有没有一种机制/工具可以提供这样的东西? bash 是否跟踪变量设置位置?

尽管这看起来似乎不是很重要,但有一天,当我帮助朋友安装 OpenCL 时,我突然想到了这个问题,并且该包应该自动设置变量 $ATISTREAMSDKROOT 。无论如何,该包应该向 /etc/profile.d 添加一个文件以允许设置变量,但事实并非如此。幸运的是,该变量显示为空白。

但我想知道如果它没有显示为空白,并且该包将其添加到某个随机文件中,除了寻找它之外,我可能无法知道它在哪里。

当然,我知道人们可以编写一两个 sed 命令并搜索脚本,但我会考虑进行详尽的搜索:D

Okay I know there is a bash debugger. But what I'm seeking is if I had an environment variable in one of my startup scripts and I don't know how it was set or where it might be, is there a way to find it other than exhaustively searching the scripts?

I mean is there a mechanism/tool that provides such a thing? Does bash keep track of variable setting locations?

Even though this might not seem very important but it crossed my mind the other day when I was helping a friend install OpenCL and the package supposedly set the variable $ATISTREAMSDKROOT automatically. Anyway the package was supposed to add a file to /etc/profile.d to allow for setting the variable, but it didn't. And luckily the variable came out blank.

But I was wondering if it hadn't come out blank, and the package added it to some random file, I would have probably had no way of telling where it is other than looking for it.

Of course I know one could write a sed command or two and search through the scripts but I'd consider that exhaustive search :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2024-10-29 03:46:59

一种选择是使用以下命令启动 bash 实例:

bash -x

... 并查找该输出中设置变量的位置。要将输出重定向到文件,您可以执行以下操作:

bash -x -ls -c "exit" 2> shell-startup-output

您应该在输出中看到每个文件的来源。

One option would be to start an instance of bash with:

bash -x

... and look for where the variable is set in that output. To redirect that output to a file, you could do:

bash -x -ls -c "exit" 2> shell-startup-output

You should see in the output where each file is sourced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文