Ubuntu: Edit environment variables via command line

1

How can you edit the environment variables via command line in ubuntu?

Ted Karmel

Posted 2010-08-14T02:36:21.133

Reputation: 131

Answers

4

You can set environment variables from the command line, but the new values will apply only to that terminal session and any processes launched from it. Environment variables are handled differently in Unix than they are in Windows. When a Unix process is created, it inherits an environment from its parent process that includes environment variables. Changes to a process's environment affect that process and its children, but not any other processes.

With that caveat, you can set an environment variable in a Bourne-like shell such as bash like this:

export MYVAR=myvalue

garyjohn

Posted 2010-08-14T02:36:21.133

Reputation: 29 085

4

Edit /etc/environment. When you log out and log back in, the new values will be present.

Eldonthian McAllister

Posted 2010-08-14T02:36:21.133

Reputation: 41

0

To set TEST to XYZ, use this format:

TEST=XYZ; export TEST

Tom

Posted 2010-08-14T02:36:21.133

Reputation: 1 321

0

I also found it useful to install nano and edit the text of the environment file which in my case was found in the etc folder.

sudo apt-get install nano

sudo nano filename

Ted Karmel

Posted 2010-08-14T02:36:21.133

Reputation: 131

I know this question is quite old, but this answer is not very useful or clear. Installing nano isn't really necessary to fix the problem, nor do you actually ever list the file you're using nano to edit. It looks like several other people posted solutions that are more thorough, you should consider accepting one. – shortstuffsushi – 2014-02-09T21:33:38.767

0

If you want to modify/set environment variable(s) for your current shell session, which will last until you close/exit it, then

export MYVAR=value

If you want to modify/set environment variable(s) to be present every time you run your shell, then you should modify your shell's config file to include the above line.

To find out what shell you're running, type:

# echo $SHELL

Then edit the appropriate configuration file:

  • For sh you would modify ~/.profile
  • For bash you would modify ~/.bashrc
  • For zsh you would modify ~/.zshrc or ~/.zshenv

Where the ~ character in each case represents the path to your home directory. If editing on the command line, you can just use it directly, e.g. to open the file in the vim editor:

# vim ~/.profile

If you're using a GUI-based editor you should just navigate to your home directory to find the appropriate config file.

drfrogsplat

Posted 2010-08-14T02:36:21.133

Reputation: 357