0

I have defined a variable by writing VAR1='abcd' into .bashrc file. I could list the all variables by using compgen -v command. I could check if any variable from this output by using printenv or echo $VARIABLE_NAME commands. This gives the result if this is a system-wide variable or shell/user-wide variable.

How could I check if this is a user-defined variable or shell variable?

Note: Maybe a search could be made in .bashrc file for the specific variable name. But there maybe a more proper way.

OS: Debian-like Linux (x64), kernel: 4.19.

demirod
  • 15
  • Related to the user's other question about classifying environment variables: https://unix.stackexchange.com/questions/640111/linux-get-list-of-local-user-system-environment-variables-seperately – Kusalananda Mar 20 '21 at 09:20
  • I don't think bash internally differentiates between variables that are set by the shell and those set by the user, but I'm not 100% sure. – Time4Tea Mar 20 '21 at 09:21
  • 1
    @Time4Tea It does not. There is, of course, a difference between shell variables and environment variables, but trying to track down exactly where these are set is problematic as there is usually no trace of what files were sourced etc. Add to that the fact that environment variables can be set for a shell in a multitude of various ways (MYVAR=hello bash -c 'printenv', for example) make it really difficult to do anything but guess the origin of a particular variable. – Kusalananda Mar 20 '21 at 09:32
  • 1
    Could you say something about why this is an issue for you? This is missing from both this and your previous question on the same topic. It would be interesting to know as it's something that is usually not an issue for most Unix users, so I thought maybe there is some other problem that you're trying to solve. – Kusalananda Mar 20 '21 at 09:33
  • My purpose is not to violate forum rules. My two questions are very similar, but not the same I think. For your question, I tried to make a GUI tool for manipulating environment variables to make it easier but, I looks very difficult to do that due to the working structure of the linux. @Kusalananda, you helped me for my two questions, thanks as a very new linux user. – demirod Mar 20 '21 at 09:39
  • If you want to change the default values, you would need to work with the startup scripts that I mentioned in my answer to your other question. If you need to change/set a large number of environment variables in a currently running shell, I think most people would write a dedicated script for that. – Time4Tea Mar 20 '21 at 12:20

1 Answers1

0

You have discovered the reason why, by tradition, variables used by the shell, or set as part of tool conf are named in all upper case (PATH, DISPLAY, LESS, ...), with all lower case names available for the user.

Environment variables (except for special shell variables (IFS, PS1, OLDPWD, PWD, ...)) are all stored in a single data structure in your shell. Environment variables are such a dynamic dataset that keeping a history would be too much overhead.

You can watch the construction of the environment by following the execution of your shell's "startup files", either manually or by inserting debugging code into the scripts.

If you have sudoedit access to the scripts (or simply ownership), you could make them controllably verbose by inserting something like:

[[ -f /tmp/debug ]] && source /tmp/debug

in a script. Then, in /tmp/debug, you can set +x, env | sort >/tmp/debug.out, etc.

When you want the debugging deluge to stop, simply rm /tmp/debug.

Read the man page for your $SHELL.

waltinator
  • 4,865