Cannot create permanant environment variable in Ubuntu - linux

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

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.

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 $PATH variable autoupdates

I have a problem on my Ubuntu 18.04 , I set PATH variable , and it works fine but after some time it reverts my changes. I do something like:
export $PATH=$PATH:/my/dir/bin .
You could add it to your .bashrc, and make it permanent.
Using any text editor, edit ~/.bashrc, and add the line you need.
Then source it, source ~/.bashrc.
With the export command the new PATH variable will be only available at the current terminal session. To make it permanent over the different sessions you will need to add it to the ~/.bashrc file.
That call will only work for your terminal session, you need to edit your ~/.profile or ~/.bashrc file, and put the path in there then start a new terminal session

Environment variable PATH on linux

Hi i am currently trying to set keywords for my terminal to launch some software without having to type the whole path.
For exemple:
firefox
#instead of
/home/debian/firefox/firefox
I always do this kind of thing on windows by setting path in the environment variable manager.
After i read this post PATH environment variable in linux , i added this line to the etc/environment file:
export firefox=/home/debian/firefox/firefox
#I also tried this:
export PATH=$PATH:/home/debian/firefox
It doesn't work, can someone explains me how to do this?
I would setup a new alias in my .bashrc or .profile, which should be located under your home directory. Add the following to the end of the file:
alias firefox="/home/debian/firefox/firefox"
Save the file and reload it using:
source ~/.bashrc
Since you added the alias to your .bashrc this alias will be created everytime you open a new instance of a shell.
You could use nohup to keep the command running after the shell session ends:
alias firefox="nohup /home/debian/firefox/firefox &"
Notice the trailing & character, which will run the command in the background so you can keep using your terminal.
You can also make an alias in your .bashrc file.
$ vim ~/.bashrc
It will open your .bashrc in read mode. Get in write mode by pressing i. You can create alias at anywhere in the file or below already created alias list.
alias firefox='/home/debian/firefox/firefox'
press Esc and then :wq
This will create your alias, save and exit the file. Now you only have to compile .bashrc by this
$ source ~/.bashrc
After this you can only have to use firefox instead of long /home/debian/firefox/firefox
Adding /home/debian/firefox to your PATH should have done it.
Did you start a new shell after making that change? Otherwise the new PATH would not have exported yet. Alternatively, you can just run export PATH=$PATH:/home/debian/firefox directly to update it for your current session.

Linux: setting environment variable with parts of another environment variable

I have an environment variable $GOPATH set in ~/.profile:
export GOPATH=$HOME/gopkgs:$HOME/code/go
Now I want to use $GOPATH to add the ./bin sub-directory of the two folders to $PATH, preferably in ~/.profile as well.
I am trying:
export PATH=$PATH:$HOME/go/bin:${GOPATH//://bin:}/bin
This only prevents me from logging into an X session. The distro is Linux Mint 11 x64.
What am I missing?
On my machine this works and
echo PATH=$PATH:$HOME/go/bin:${GOPATH//://bin:}/bin
substitutes to:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/home/l1zard/go/bin:/home/l1zard/gopkgs/bin:/home/l1zard/code/go/bin
Therefore i would guess that you have an error in your .profile.
however using the less complicated
export PATH="$PATH:$HOME/gopkgs/bin:$HOME/code/go/bin"
does not use fancy variable substitution but it should work. Also you do not need to log off and on again to make things work. you just need to source the .profile by typing:
source ~/.profile
This way you can also make sure that the .profile has no errors which might lead to not being able to log in to an X session.

Resources