Ask Your Question

dynamically set and change ENV variable, systemwide

asked 2019-01-06 18:39:15 -0600

tmdag gravatar image

updated 2019-01-07 10:38:22 -0600


I'm setting env variable as usual:

$ setenv MYPROJECT currentproject

But when I open new terminal window/tab, my env is destroyed and no longer available.

MYPROJECT: Undefined variable.

I want dynamically (via scipt) change/set env variable that will be available under all terminal windows. I am using it to set what current project I am working on, and I want this variable to be available to every application system wide. And of course I can change which 'project' I am working on by changing $MYPROJECT

How can I achieve that ?

edit retag flag offensive close merge delete


What conditions determine the environment variable? You say you want to set dynamically, but do not give an example of the variable you are setting.

SteveEbey73701 gravatar imageSteveEbey73701 ( 2019-01-07 10:25:01 -0600 )edit

There is no really more to it. When I am working on a project, I want to set my env variable $MYPROJECT to that project name. And at the same time, I want all my apps on the system to retrieve that information (check that currently i am working on $MYPROJECT). I will be changing this variable to different if I am working on a different project of course

tmdag gravatar imagetmdag ( 2019-01-07 10:35:43 -0600 )edit

How do you currently set the project variable? Do you manually do it, or is the project name in a file, that could be used as input for a script to use?

SteveEbey73701 gravatar imageSteveEbey73701 ( 2019-01-07 11:36:47 -0600 )edit

I am manually sourcing script with 'setenv $MYPROJECT $argv[1]'.

It works 'well' within a realm of terminal where it was sourced from, but not outside. I would like to set this variable even from 'stand alone' QT app (which would source that setenv script when button pressed)

tmdag gravatar imagetmdag ( 2019-01-07 12:42:54 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-01-07 13:51:10 -0600

sideburns gravatar image

When you start a new shell, it sources /etc/bashrc to set the default variables, which can then be modified as Aeperezt wrote. Once the shell is active, there's no way to change those variables from the outside, even by root. This is not a bug, it's a security feature, because it prevents any user (Remember, Linux and Unix were developed to be used by multiple concurrent users.) from messing up anybody else's session.+

edit flag offensive delete link more


thanks! I guess I could set info on shared file which would be sourced by every, single bashrc

tmdag gravatar imagetmdag ( 2019-01-07 19:28:36 -0600 )edit

answered 2019-01-06 19:48:23 -0600

aeperezt gravatar image

If that is your user settings you should add your environment variables on the .bash_profile or .bashrc file setting on one of those file should work for you. If you want to be on all users then you will have to add it on the /etc/bashrc

Good Luck

edit flag offensive delete link more


thx man, but that is not a way to DYNAMICALLY set env variable. What you are describing is hard-coded env variable set each time new terminal is open.

tmdag gravatar imagetmdag ( 2019-01-06 20:40:32 -0600 )edit

@sideburns explain it very well, you can set up variables but they will not transcend your session and current shell. Then only way to do so is as I have explained. It is not a limitation is is a security feature.

aeperezt gravatar imageaeperezt ( 2019-01-07 15:45:34 -0600 )edit

Question Tools

1 follower


Asked: 2019-01-06 18:39:15 -0600

Seen: 109 times

Last updated: Jan 07 '19