Find out where an environment variable was last set in bash

前提是你 提交于 2019-12-30 01:08:16

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5299886/find-out-where-an-environment-variable-was-last-set-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!