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

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

Related

AWS Environmental variable independent of shell

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.

Default bash environment changed. How do I revert it to default?

I am using AWS EC2 along with the CodeStar services.
I wanted to add the path to maven's binary to the PATH environment variable.
So I wrote it to the /etc/environment file and executed the command:
source /etc/environment
But it has now changed the default environment of the bash shell and now I am not able to execute any command. Even for a command like ls it gives output -bash: command not found
How do I revert back to the default settings. I tried whatever my brain could think of. But nothing helped. Would be glad to hear from the community.
In order to revert back to the default settings:
As #Cyrus wrote you should update the PATH variable
In the bash shell run the following command:
PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin"
Undo the changes to the /etc/environment
edit the /etc/environment using the same method you used before, and remove the PATH line you added to the file
You can use sudo vi /etc/environment or any other editor you prefer
Note: In my AWS machine, the /etc/environment file is empty.

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).

Unable to Set VISUAL environement variable in bashrc

I am trying to set the VISUAL and EDITOR environment variables in a server for default usage with crontab.
When I do a manual export VISUAL=vim, things work perfectly.
But when I add the lines
EDITOR=vim
VISUAL=vim
to my .bashrc file and logout and re-login, I don't see any changes on opening crontab -e.
If later I do echo $VISUAL, I get the response vim
What am I doing wrong here?
You have to put:
export EDITOR=vim
export VISUAL=vim
in your .bashrc file to make the variables available to the subprocess.
Works perfectly in my environment ubuntu 12.04.!

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