For example, when I run export export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
or a GOPATH=~/workspace/me/go
which file does this get added to?
For example, when I run export export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
or a GOPATH=~/workspace/me/go
which file does this get added to?
When you
export
a variable, it is not added to any file anywhere.The effect of
export
, as opposed to merely assigning a variable, is that of passing the variable into the environment of all of the shell's child processes. It does not do anything else; it does not modify any of the shell's configuration files. After the shell and all its child processes have exited, the exported variable is gone! When you open a new shell that is not a child of the first shell, that shell will not remember your variable.In fact, there is no need to
export
PATH because PATH is already an environment variable. It is already exported; it is automatically going to be passed into the environment of child processes, and when you change it, child processes will inherit the change too.You can see this using
printenv
, a command to print environment variables:(Now I need to exit that shell to get my PATH back, or
source /etc/environment
, because any new shell I start from this shell will inherit that PATH variable I have messed up...)If you want to set any variable persistently, you need to add it to one of the shell's configuration files yourself.
PATH is the only variable, as far as I know, that is by default set in
/etc/environment
on Ubuntu. I recommend not editing/etc/environment
to set the PATH, or if you do, be careful to make a backup of the original file, because you will have no other way of recovering the original PATH (unless you wrote it down somewhere or have an awesome memory). Also be aware that no expansions will be performed on the contents, so you must use full paths, not$PATH
or$GOPATH
or~
. If you write such things, they will be literally in the PATH, and if your PATH is literally$PATH:~/workspace/me/go
, you will not be able to run any commands (except those built into the shell) without using their full paths.I also do not recommend setting PATH in
.bashrc
, because every interactive Bash shell reads.bashrc
, and it's quite common to start a new shell from a shell, and so on... If you put something likein your
.bashrc
, you will then find that/home/user/foo
gets appended to your PATH multiple times; each time a shell starts a shell, the PATH gets extended again. This could eventually make things slow down, especially if you added a lot of paths. Also, only interactive Bash shells read~/.bashrc
, so if you use a shell other than Bash, or run a script, without it being a child process of an interactive Bash shell, it won't have this variable.Instead I recommend adding a line to
~/.profile
to extend your PATH. This file is only read by login shells; typically, it is only read once when you log in to your session, so the PATH will only be appended to once, and all shells will inherit environment variables set there, not only interactive Bash shells. It is usual to add environment variables to this file. So, you can open~/.profile
in a text editor, and add lines saying something like:It is a good idea to quote the path as shown, in case any of the directories include special characters (it's also a good idea to create directories that don't have special characters in their names, but, things happen), and if you do quote it, you cannot use
~
as a shortcut for/home/user
because double quotes ("
) suppress tilde expansion, but you can use$HOME
instead because parameter expansion is still performed and$HOME
will become/home/user
.After editing, save the file and exit, log out and back in, or run
source ~/.profile
, and your PATH will always include/home/user/workspace/me/go
.Shell custiomizations are usually edited into
$HOME/.bashrc
, which issourced
by each shell you start. Readman bash
.BTW, You must have the
GOPATH
definitionBefore you use
GOPATH
Also, where is the definition of
GOROOT
?