Cannot add new environment variable

I need to save value of some variable during the session, but adding

export MY_NEW_VAR=$(some-command-or-shell-script)

in ~/.bash_profile changes nothing (echo $MY_NEW_VAR - empty). However in a case for example with a variable PATH :

export PATH=$PATH:/jone/bin

all Ok:

echo $PATH
/home/jone/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/jone/bin

In what a difference between these cases?? And how to me to make that I want?

Try to use ~/.bashrc or ~/.profile .

Or maybe the $(some-command-or-shell-script) fails for some reason?
Does “export MY_NEW_VAR=xxx” work as expected?

UPD
I make it in a subprocess in ~/.bash_profile

_graphical_target () {
    export MY_NEW_VAR=$(/path/to/script)
}
 (sleep 2
  -n "$(xset -q)" ] && _graphical_target) &


Maybe an error in this?

When someyhing is set in a child process, this will never be “back-warded” to the parent process. After the child process exits, it is gone.

~/.bashrc doesn’t approach - “I need to save value of some variable during the session”, but bashrc works only for current terminal (?)
~/.profile hmm… it is necessary to check

Ok, I will try to rewrite. Thanks.

.bashrc is sourced whenever a new shell is started.
Yes, it only “works” for children of this shell process. But that’s the same for ~/.profile or ~/.bash_profile.
The only difference is that ~/.profile or ~/.bash_profile is only run when a new login shell is started, whereas .bashrc is run for every new shell.

Anyway, as I verified meanwhile, ~/.bash_profile should work fine.
Your problem is indeed likely because you set the variable in a subprocess.

You seem to be confused about what “your session” is. In your terminal, you run a CLI session.

I also get the idea that you missed an important thing to consider when you ask questions: How To Ask Questions The Smart Way

hcvv, Ok :slight_smile: If to speak simply, I’m making a replacement for gnome-keyring. Therefore value (it is the password) shall be kept only in memory. That’s all))