找出 bash 中最后设置环境变量的位置
好吧,我知道有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是使用以下命令启动 bash 实例:
... 并查找该输出中设置变量的位置。要将输出重定向到文件,您可以执行以下操作:
您应该在输出中看到每个文件的来源。
One option would be to start an instance of bash with:
... and look for where the variable is set in that output. To redirect that output to a file, you could do:
You should see in the output where each file is sourced.