Environment variable always reverts to old value - linux

I'm using Linux and want to update an environment variable. I set a new entry with
export theKey=theOldValue
and checked with
printenv
Now I have a new value for this key and want to update it. I wasn't able to find any "update" commands so I run the export command again. This seems to update the entry when running printenv but whenever I try to read the value from my code I get the old entry value.
Whenever I change the value I reboot the machine.
Since this isn't working properly I tried the following
unset theKey
export theKey=theNewValue
reboot
When checking the value this variable reverted back to the old value. Does someone know what I'm missing?

The export command has the effect in the one terminal session you run it only. So you need to touch ~/.bashrc if you are using bash, and put your export entry in that file. Then to apply it in that session, do source ~/.bashrc. For future terminal sessions, this file will be loaded automatically.
See more info here.

Related

How can I save an ENV VAR on Linux Ubuntu 16.04?

I typed on my terminal the following command :
export BACKEND_URL="<confidential>"
. Apparently it was working, but when I turned off my computer and turned on again, I realized that this env var named BACKEND_URL It was not saved anymore. Why this happened ?
First of all:
This question would better fit the Unix&Linux StackExchange site than this web site.
However:
By typing "BACKEND_URL="<confidential>"" you set a variable which is valid for the currently running shell (terminal window) only.
If you close the terminal window, the variable is gone. The variable is also not valid for programs started from that terminal window.
If you add export (as you did), the variable is also valid for programs started from that terminal window.
However, still it is not valid for other terminal windows and if you close the teminal window, it will be gone.
When using one of the standard shells, you can add variables to the .profile file in your home directory.
When you are using bash, you can also add variables to the .bashrc file in your home directory.

EMCC not found - only works in emsdk not globally in terminal

I am a new Linux user and am looking to get the emscripten emcc command to work globally on Ubuntu.
This is my current configuration:
LLVM_ROOT = '/home/mpaccione/Projects/emsdk/upstream/bin'
BINARYEN_ROOT = '/home/mpaccione/Projects/emsdk/upstream'
EMSCRIPTEN_ROOT = '/home/mpaccione/Projects/emsdk/upstream/emscripten'
NODE_JS = '/home/mpaccione/Projects/emsdk/node/12.9.1_64bit/bin/node'
TEMP_DIR = '/tmp'
COMPILER_ENGINE = NODE_JS
JS_ENGINES = [NODE_JS]
If I am in /var/www/html/collision-detection-wasm/hello-world
... command 'emcc' not found.
I followed the install instructions but they did not work how I would expect. I need this to work globally or it's not of use.
How do I go about doing that on Ubuntu Linux?
In order to use emcc it needs to be in your $PATH.
To do this you can run source /path/to/emsdk/emsdk_env.sh in your terminal.
Or if you want to make it permanent you can add that same command to your startup scripts (e.g. $HOME/.bash_profile or $HOME/.bashrc).
This is documented at https://emscripten.org/docs/getting_started/downloads.html.
Hijacking #sbc100's answer to further clarify things to answer OP's question asked as a comment.
In order to use emcc one needs to set the PATH and other environment variables.
As it can be seen in the documentation, the command source ./emsdk_env.sh (or source /path/to/emsdk/emsdk_env.sh if you are not within the emsdk directory) does exactly that for the current terminal.
If you want this effect to be permanent, then do just as #sbc100's said. Just add it to, for example, to your bashrc file by inserting source /path/to/emsdk/emsdk_env.sh to a new line at the end. This will make the script run each time a terminal opens. This may be annoying to same as it outputs what was added to PATH and which environment variables were set.
Alternatively, one may also add the entries to the bashrc manually that the script kindly tells us about. For me, it meant inserting the following lines:
## Emscripten ########
export PATH="/home/dudly01/repos/github/emsdk:$PATH"
export PATH="/home/dudly01/repos/github/emsdk/upstream/emscripten:$PATH"
export PATH="/home/dudly01/repos/github/emsdk/node/14.18.2_64bit/bin:$PATH"
export EMSDK="/home/dudly01/repos/github/emsdk"
export EM_CONFIG="/home/dudly01/repos/github/emsdk/.emscripten"
export EMSDK_NODE="/home/dudly01/repos/github/emsdk/node/14.18.2_64bit/bin/node"
I would think, however, that these lines need to be adjusted as the project evolves.

my fedora PATH variables went out, how to restore them

I wanted to add php to envirmonment variables
So I wrote:
export PATH=/opt/lampp/bin/
Then I discovered that this was wrong because it relaced the envirmonment variable PATH with only ( /opt/lampp/bin/ ).
Is there a way to restore the path to an earlier version?
restart your shell / exit your terminal. unless you edit your .bashrc file the changes to environment variable via the export command are not permanent

customed command not found in a new terminal

everyone.
I had a basic question want to consult, about the environment variable setting.
After closed my one existed terminal which could execute compile(make) and do customed(mksdboot) command, i can't do mksdboot command anymore(I had execute a predefined setting environment variable shell script i.e. $ . ./arndale_envsetup.sh again) in the new terminal.
Cause i am a beginner in Linux, i am not very clearly about the environment variable setting rules.
i had tried to 'su' or 'sudo' to execute mksdboot, but no luck:(
ps. I had another project needs to compile in my PC(i didn't export PATH to .bashrc, only execute export PATH when i open a new terminal every time), may it efforts the original project's environment variable?
thanks.
[UPDATED]
i tried using $source ./arndale_envsetup.sh, relative commands worked finally.
but i still did't figure out the reason between work or not work. >"<
The command
history
will list what your previous commands where.
This might give you a pointer what set the path in the way you needed it.
You could also try to see where you command is via
locate mksdboot

global variable but still can't access in linux

i have trying to use some global variable in my ant file.
when i do login through terminal. i can access those variable like JAVA_HOME
but when i am trying to access variable through the ant command i am not able to find them.
global variable declared in .cshrc
setenv JAVA_HOME jdk_full_path
ant code using variable.
<property environment="env"/>
<property name="ear" value= "true"/>
<property name="home" value="${env.HOME}"/>
<property name="java_home" value="${env.JAVA_HOME}"/>
i can access home variable but i am not able to find JAVA_HOME variable ,i am executing this ant through eclipse
Please suggest me where should i declare the variable so i can access them
The problem is probably that Eclipse doesn't have JAVA_HOME in its environment.
Try logging out and back in again, then run Eclipse.
Alternatively, open a terminal, and run Eclipse from there.
The .cshrc file is only run when you start a new C shell, it is not for global variables. Since you are not starting Eclipse from the C shell it will not see any variables you set there.
Try the ~/.login file or the /etc/profile file in case eclipse starts up using bash. Both of these files are only loaded once when the user logs on so they should effect everything. You may require a restart or even a reboot for changes to these files to take effect.
Environment variables are inherited through process execution, they are not global. So, when you edit your shell's RC file to add a variable, it only takes effect for shells executed AFTER that modification, and for programs executed from those shells. If you started Eclipse from KDE/Gnome, and KDE/Gnome was started before you made that change, then KDE/Gnome never had the definition and therefore Eclipse did not either.
I recommend logging out and back in. Also, depending on your default shell setting, you may find that KDE/Gnome is being started through bash or something else, meaning that you'll need to modify .bashrc instead of .cshrc.
You should define this variables at one of the following files:
~/.profile or ~/.bash_profile runs only with login shells i.e when you first log in into system.
~/.bashrc file runs every time you open a new non-login bash shell such as xterm
So, you should add to this files the line:
export JAVA_HOME=jdk_full_path
You must re-login for the changes take efect

Resources