AWS Environmental variable independent of shell - node.js

I am new in AWS
I am using an Ubuntu server and I use export in the shell to set environmental variables.
export mykey=value
But when I close the shell and relogin I see that mykey does not available the environmental variable. As far as I search in the net, such way of setting will expire directly after shell is closed. How could I have a permanent environmental variable in AWS then?
Thanks

You could edit the ~/.bashrc file by adding export mykey=value command to have it every time the shell reloads. Environmental variables are not saved when you exit all the instances of the shell. But everytime shell opens, it reads the bashrc file, if you are in a bash shell. For zsh, it would be ~/.zshrc for example.

Related

how to avoid enviroment variable from resetting in WSL terminal after restart?

environment variable set (in zsh shell) in WSL disappears after restarting the terminal. I have used
export variable_name=variable_value
to set environment variable. how should I avoid env variable from resetting after restart.
I added environment variable in .zshrc file after reading comment of #Biswapriyo.
like this-
export variable_name=variable_value
and now it works.
I think, at every startup, scripts in .zshrc file are ran so this env variable is addded. If you use bash instead of zsh then you may try adding this in .bashrc file or any other rc file. You can check if env variable is added or not by
printenv

Linux Environment Variables

I'm trying to change my environment variables using a shell script, but the shell script isn't changing it.
Sorry for asking that type of question, but I'm learning about Linux, I just can't figure out what I'm doing wrong here..
Here's the script
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/jre/bin
After I execute this .sh file, when I type
echo $PATH
It doesn't return the new PATH I setted
Instead of executing it like
$ ./script.sh
source it with
$ source script.sh
The first variant creates a new process which does not pass back its modified environment variables to the calling process, while sourcing just executes the script within the already running shell.
See also What is the difference between executing a bash script and sourcing a bash script?
As Andreas explained, you need to source the definition (and you need the export to happen in your current shell process, not in a subshell or child process)
You could instead define in your ~/.bashrc some shell functions to do the job:
## remember the original path at start of the interactive bash
export ORIGINAL_PATH=$PATH
function prepare_for_java() {
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60
export PATH=$ORIGINAL_PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
}
function forget_java() {
unset JAVA_HOME
export PATH=$ORIGINALPATH
}
then in your terminal you would type
prepare_for_java
before starting doing Java things, and
forget_java
after you've done with them.
I would suggest to read the Advanced Bash Scripting Guide and Advanced Linux Programming (to get a wider picture).
See also bash(1), credentials(7), fork(2), execve(2), environ(7)

Cannot create permanant environment variable in Ubuntu

I'm trying to create a permanent environment variable on Ubuntu.
I tried the following:
export SASS_LIBSASS_PATH=/usr/local/lib/libsass
When I opened a new terminal and used 'printenv', I found the variable disappeared!
I also tried to make it work by appending:
SASS_LIBSASS_PATH="/usr/local/lib/libsass"
to the end of the file in /etc/environment, and then used:
source /etc/enviroment
That did not work!
UPDATE:
I then tried editing my ~/.profile file:
SASS_LIBSASS_PATH="/usr/local/lib/libsass"
that did not work.
I then tried instead appending the following at the end of ~/.profile file:
export SASS_LIBSASS_PATH="/usr/local/lib/libsass"
that too did not work!
How can I make a permanent system variable?
export only sets the environment variable in your current shell session and any child processes started by that shell session. It is certainly not "permanent". The only way to set an environment variable for future shell sessions is to add the export command to a shell start-up file. Your best bet is probably to put it in ~/.profile (unless the file ~/.bash_profile exists).

Linux: export environment variable in a shell script to make it flexible on any server

In order to run a Tcl script on Linux, I need to set the environment variable "$LD_LIBRARY_PATH" each time.
For convenience, I develop a shell script to do this.Currently, on my own server, if I type
echo $LD_LIBRARY_PATH
the result is:
/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib
so in my shell script I write the following code:
export LD_LIBRARY_PATH="/opt/lsf/9.1/linux2.6-glibc2.3-x86_64/lib:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
where the "$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64" part is what I want to add. It works well. Now the issue is:
If I want to run the script on any server, so the original "$LD_LIBRARY_PATH" will be different, based on my understanding. So how to make it flexible on any server?
I try this in my shell script:
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64"
But not so sure,
I am new to system stuffs, need some help. Hope explain the issue clearly.
If your default shell is bash, i would define the variables in ~/.bashrc like:
export INSTALL_ROOT=...##assuming a lready defined
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INSTALL_ROOT/tcl_tk/lib64:$INSTALL_ROOT/tcl_tk/lib64
So in this way, you dont have to worry anything about setting variables in multiple shell scripts as this .bashrc is going to setup variable for you beforehand.

Make $JAVA_HOME easily changable in Ubuntu [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
In Ubuntu, I'd like to switch my JAVA_HOME environment variable back and forth between Java 5 and 6.
I open a terminal and type in the following to set the JAVA_HOME environment variable:
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
And in that same terminal window, I type the following to check that the environment variable has been updated:
echo $JAVA_HOME
And I see /usr/lib/jvm/java-1.5.0-sun which is what I'm expecting to see. In addition, I modify ~/.profile and set the JAVA_HOME environment variable to /usr/lib/jvm/java-1.5.0-sun.
And now for the problem--when I open a new terminal window and I check my JAVA_HOME environment variable by typing in echo $JAVA_HOME I see that my JAVA_HOME environment variable has been reverted back to Java 6. When I reboot my machine (or log out and back in, I suppose) the JAVA_HOME environment variable is set to Java 5 (presumably because of the modification I made in my ~/.profile).
Is there a way around this so that I can change my JAVA_HOME environment without having to log out and back in (AND make that environment variable change stick in all new terminal windows)?
Put the environment variables into the global /etc/environment file:
...
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
...
Execute "source /etc/environment" in every shell where you want the variables to be updated:
$ source /etc/environment
Check that it works:
$ echo $JAVA_HOME
$ /usr/lib/jvm/java-1.5.0-sun
Great, no logout needed.
If you want to set JAVA_HOME environment variable in only the terminal, set it in ~/.bashrc file.
This will probably solve your problem:
https://help.ubuntu.com/community/EnvironmentVariables
Session-wide environment variables
In order to set environment variables in a way that affects a particular user's environment, one should not place commands to set their values in particular shell script files in the user's home directory, but use:
~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.
Not recommended:
~/.profile - This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.
Try these steps.
--We are going to edit "etc\profile".
The environment variables are to be input at the bottom of the file. Since Ubuntu does not
give access to root folder, we will have to use a few commands in the terminal
Step1: Start Terminal. Type in command: gksudo gedit /etc/profile
Step2: The profile text file will open. Enter the environment variables at the bottom of the page........... Eg: export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java
export PATH=/home/alex/jdk1.6.0_22/bin:$PATH
step3: save and close the file. Check if the environment variables are set by using echo command........ Eg echo $PATH
You need to put variable definition in the ~/.bashrc file.
From bash man page:
When an interactive shell that is
not a login shell is started, bash
reads and executes commands from
/etc/bash.bashrc and ~/.bashrc, if
these files exist.
Traditionally, if you only want to change the variable in your terminal windows, set it in .bashrc file, which is sourced each time a new terminal is opened. .profile file is not sourced each time you open a new terminal.
See the difference between .profile and .bashrc in question:
What's the difference between .bashrc, .bash_profile, and .environment?
.bashrc should solve your problem. However, it is not the proper solution since you are using Ubuntu. See the relevant Ubuntu help page "Session-wide environment variables". Thus, no wonder that .profile does not work for you. I use Ubuntu 12.04 and xfce. I set up my .profile and it is simply not taking effect even if I log out and in. Similar experience here. So you may have to use .pam_environment file and totally forget about .profile, and .bashrc. And NOTE that .pam_environment is not a script file.
Take a look at bash(1), you need a login shell to pickup the ~/.profile, i.e. the -l option.
I know this is a long cold question, but it comes up every time there is a new or recent major Java release. Now this would easily apply to 6 and 7 swapping.
I have done this in the past with update-java-alternatives:
http://manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html
After making changes to .profile, you need to execute the file, in order for the changes to take effect.
root#masternode# . ~/.profile
Once this is done, the echo command will work.

Resources