2 ways to permanently set $PATH variable in ubuntu
The $PATH
variable is one of the default environment variable in linux (ubuntu). It is used by the shell to look for executable files or commands. Although there are two types of environment variables - global and local, I'll discuss here only this specific $PATH
variable.
So, let us start by first outputting the content of $PATH
variable:
$ echo $PATH
And you'll get the result something like this which is a list of directories separated by colon:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Now here comes the important part to make your terminal programs executable without writing full path.
1. Exporting PATH variable to /etc/environment
One of the fastest way to permanently add directory to $PATH
environment variable is by using the following command:
# first append the new directory to path
$ PATH = /usr/local/sbin:/usr/local/bin:/pathToMyDirectory
$ source /etc/environment && export PATH
2. Using ~/.profile
file
Another way is use the .profile
file by adding the export
command and then run the source
command:
# add this command to `~/.profile` file
$ export PATH=$PATH:/myNewDir
# then run the source command
$ source ~/.profile
Here $PATH
refers to content already set by system, so that we don't have to write previous directories path.