Struggling to unset an environment variable in bash - environment-variables

I have previously set an environment variable using:
echo export "AVARIABLE=example" >> ~/.bash_profile
but now after using:
unset AVARIABLE
the env var remains when I open a new shell? What am I doing wrong here? Even running:
source ~/.bash_profile
does not work?

If you are opening a new shell, about the first thing it does is to source ~/.bash_profile. And there, the variable is set again.
If you want to get rid of it permanently, edit your ~/.bash_profile to remove the line in question again. (This will only take effect for new sessions.)
If you only want to unset it in your current shell, then unset is fine but as you've seen, it won't affect new invocations of the shell.

Related

why environment variable removed from my OS?

I have several environment variable from a text.txt file , i set them with export variable = value manually one by one myself in terminal Ubuntu 18.04 but now no one of them appear in printenv !
I need to set them somehow never remove again(unless i delete them myself).any idea?thank you
Add your variables in ~/.bashrc or in /etc/environment this way on every reboot they will be exported.
in bashrc should be like this
export VARIABLE='<value>'
in /etc/environment
VARIABLE=<value>

Unable to set environment variables using source ~/.profile in script

So I have a script, and I'm setting an environment variable using:
echo 'export env_var=hello' >> ~/.profile
source ~/.profile
When the script completes, I check the existence of this environment variable by doing:
echo $env_var
Which gives me "" rather than "hello". If I run source ~/.profile on my terminal (typed straight up in the terminal, not called from any script) , the environment variable will be set correctly. Why is my script unable to set the environment variable correctly, and what do I need to do for it to do so?
env_var is being export-ed all right, but in the subshell (and its children) the script is being executed on.
When you execute an= script, the script is run on a subshell, which is a child of the calling shell, so after the script finishes, the exported variables go out of scope from calling shell's point of view.
A common practice to have all the variables (and other data structures) available in the calling shell, is to source (.) the script, not execute it, which you are doing but from the script, not from the interactive session.

Ubuntu: Environment variable is deleted after closing session

I am setting an environment variable in Ubuntu 14.04 for a script to use it.
I opened the terminal and did:
export VARNAME=/home/me/folder/folder2
And then run the script and everything works fine. But anyway as soon as I close my session, the variable seems to disappear and I have to declare it again like the first time.
To set an environment variable that doesn't get erased with the closing of the terminal (Ubuntu 16.04), follow these steps:
Open .bashrc file by using the text editor of your choice. For me, it was
code ~/.bashrc as I use VS Code, but you can use vi ~/.bashrc or subl ~/.bashrc.
Add the environment variable using export VARNAME=/home/me/folder/folder2
Save the file and close.
The variable will persist even after the terminal is closed.
Actually if you set the variable via terminal it will last till shutdown. If you want to set permanent variable you have to do the following.
$ vi ~/.bash_proflle
// set the variable in the file
exit by pressing esc key and type :wq Now the path is set.

Unable to fix rails smtp.rb error

I followed the post here,
Rails 3 - Devise/ActionMailer/RUBY-SMTP causing a segmentation fault
But, trying to add this variable to my home directory's .bashrc or .zshrc or .bash_profile does not seem to provide this variable. After adding RUBYOPT to these files, I tried to echo $RUBYOPT, but nothing comes on the screen.
Do I need to restart my laptop or add this variable somewhere else?
do the following:
$ echo 'export RUBYOPT="-ropenssl"' >> ~/.bashrc
$ source ~/.bashrc
and then see if you have RUBYOPT is set or not

How to prevent Tmux from filling up the global PATH variable with duplicated paths?

I'm using Mac OS X, iTerm2, zsh and Tmux via Homebrew. When I start a Terminal session in iTerm2, the global PATH variable looks still fine. But when I open up a Tmux session the PATH variable is extended with the same paths it already consisted of. I'm going to put a issue solving code snippet in my .zshrc, but I'm still interested in the cause why the PATH variable is filled up twice.
This happens because your .zshrc is evaluated for every new zsh process. So when you start iTerm2 it gets evaluated making your changes to $PATH, then when you start tmux that gets your modified $PATH and passes it on to a new instance of zsh inside of there and that new zsh process again evaluates the .zshrc making the changes again.
There are several ways that you could prevent this.
$TMUX
First, to specifically prevent it from happening for shells inside of tmux you could skip making those changes if $TMUX is set:
if [[ -z $TMUX ]]; then
PATH="$PATH:/foo"
fi
zprofile
Another option would be to move that portion of your .zshrc to your .zprofile file. This file is only evaluated by login shells. But, by default tmux starts new shells as login shells, so you'd also need to prevent tmux from doing that by adding the following to your tmux configuration:
set -g default-command /bin/zsh
You may need to adjust the path to zsh there. This would prevent tmux from starting zsh processes as login shells, so zsh inside of tmux wouldn't look at the .zprofile.
typeset
Another option somewhat along the lines of the code snippet that you linked to for preventing duplicates to be added would be to change your path modification to be something like:
typeset -aU path
path=( $path /foo )
This works because zsh automatically sets up the $path variable as an array that mirrors the content of $PATH. The -U option to typeset modifies that variable so that the entries are unique.
I found this GitHub thread very useful. The solution from this comment worked for me:
# /etc/zshenv
if [ -x /usr/libexec/path_helper ]; then
PATH="" # Add this line
eval `/usr/libexec/path_helper -s`
fi
By doing this, you'd have to put your PATH modifications in ~/.zshrc instead of ~/.zprofile. I also tried this solution from the thread but didn't work for me.
My solution:
Step 1:
In .bashrc or .zshrc
ExtraPath="/foo/bar:$HOME/bin" # your customized path here, /foo/bar and $HOME/bin for instance
if ! [[ "$PATH" =~ "$ExtraPath" ]] ; then PATH="$ExtraPath:$PATH" ; fi # if the PATH does not contain your customized path, then insert yours, else do nothing.
Step 2:
in ~/.tmux.conf, add
set -g default-command "${SHELL}"
in this case, tmux will not source /etc/profile, so it will not mess up with your PATH

Resources