找出最后在 bash 中设置环境变量的位置 [英] Find out where an environment variable was last set in bash

查看:23
本文介绍了找出最后在 bash 中设置环境变量的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

尽管这看起来不是很重要,但前几天我在帮朋友安装 OpenCL 时突然想到了这个包,据说该软件包会自动设置变量 $ATISTREAMSDKROOT.无论如何,该软件包应该向 /etc/profile.d 添加一个文件以允许设置变量,但它没有.幸运的是,这个变量是空白的.

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.

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

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

推荐答案

一个选项是启动一个 bash 实例:

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.

这篇关于找出最后在 bash 中设置环境变量的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆