1

I could get system environment variables by using printenv command, but I need separate variables data:

How could I lists of get local (session-wide), user (user-wide) and system (system-wide, global) environment variables separately?

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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
demirod
  • 15
  • 1
    There is no way to tell these apart, there are only environment variables. – Kusalananda Mar 19 '21 at 16:23
  • @Kusalananda♦, How could I understand which is system-wide or user-wide? It is not compulsory to use printenv command. There are some commands for defining variables as user-wide, system-wide or local (session-wide), but I couldn't find how to list them. Note: I am a new Linux user. – demirod Mar 19 '21 at 16:27
  • 1
    There are no separate commands for defining environment variables as "system wide", "user wide" and "session wide". Depending on your Unix, some environment variables may be set as default variables for all commands, others may be set in default shell startup files, other may be set when the graphical desktop starts, others set for individual command, others set for your personal shell sessions, others set for particular shell sessions, others set for one single command from the command line. There is no categorization of these variables into "system", "user" or "session". – Kusalananda Mar 19 '21 at 16:38

1 Answers1

2

I think you have a misconception about how Linux environment variables work. Environment variables for a running shell are only defined for that instance of the shell that is running. They have no meaning or relevance outside of that. If you change the $PATH variable in a shell you are using, that change will only have an effect on that instance of the shell, not on others that you may have running.

When a shell starts up and the user logs in, environment variables can be set by various shell scripts, which may define default environment variables on a system-wide or per-user basis. For bash, these are scripts like /etc/profile (system wide) or ~/.bash_profile or ~/.bashrc (specific to the user). As far as I know, there is no way to determine from a running shell where a particular variable was set - you would need to check those files.

Another concept you should be aware of is of exporting variables. The export command in bash can be used to flag which variables should be exported to new sub-shells that the running shell may create.

Also, be aware that environment variables are specific to specific shell programs, they are not global for the Linux system. So, variables for bash (which I have been using as an example) may be different to those used in csh (although there may be some similarities) and/or they may be set to different defaults.

Time4Tea
  • 2,366