4

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?

Zanna
  • 70,465
Kaigo
  • 515

2 Answers2

10

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:

$ foo=bar; echo $foo             # set a variable   
bar                              # the shell knows it
$ printenv foo                   # but it's not in the environment...
$ export foo; printenv foo       # export the variable and check again
bar                              # there it is!
$ foo=new; printenv foo          # change the variable
new                              # the change is inherited
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ PATH=/usr/bin; printenv PATH   # printenv is in /usr/bin so keep that!
/usr/bin                         # the change was inherited

(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 like

PATH="$PATH:$HOME/foo"

in 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:

GOPATH="$HOME/workspace/me/go"
PATH="$PATH:$GOPATH/bin"

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.

Zanna
  • 70,465
0

Shell custiomizations are usually edited into $HOME/.bashrc, which is sourced by each shell you start. Read man bash.

BTW, You must have the GOPATH definition

 GOPATH=~/workspace/me/go  

Before you use GOPATH

export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Also, where is the definition of GOROOT?

waltinator
  • 36,399
  • Just my incomplete example thats all, I don't have issues with go. I was just wondering were it save thats all! – Kaigo Apr 13 '18 at 18:14
  • To be clear, just running export from your bash terminal won't save things anywhere, all it means is those changes will be applied for your current session, and will be reverted when you close the terminal window. You need to actually edit $HOME/.bashrc and add those commands in there for them to remain. – habs Apr 13 '18 at 20:53