Add directory to path permanently

I ssh in and am trying to add a directory permanently to $PATH in centos 7 and having issues. My current $PATH is

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

but when I do a sudo nano ~/.bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

In ubuntu 14.04 I would do the below and that worked great, but it doesn't seem to in centos 7 . Thank you :).

export PATH="$PATH":/usr/bin/gatk-4.0.10.1
source ~/.bashrc

I don't see where you added /usr/bin/gatk-4.0.10.1 to your PATH in the .bashrc. I won't ask why it's in /usr/ and not /usr/local/, though :slight_smile:

1 Like
export PATH=$PATH:/usr/bin/gatk-4.0.10.1
source ~/.bashrc

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

is this not correct? II don't see where the /root/bin is coming from either. Thank you :slight_smile:

I use /usr/bin to make it easier for others on the server to find. Thank you.

update

export PATH="$PATH":/usr/bin/gatk-4.0.10.1

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/bin/gatk-4.0.10.1

but then after exiting and re-logging in

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

Perhaps I misread your first post. Did you actually edit your .bashrc to add the path to gatk-4.0.10.1, or just change it on the command line?

(second, probably silly question - are you using bash?).

/root/bin was most likely added to PATH from some default bashrc in /etc, or in .bash_profile in root's home directory.

1 Like

I just ran the below on the command line. Is there a better more optimal way? Thank you :).

export PATH="$PATH":/usr/bin/gatk-4.0.10.1

I am using bash although that is a bash command to add to path I think. Thank you very much for the clarification and help:).

Well, if you modify PATH from the command line it will be lost when you end your current session. To make it stick, you need to modify the path in one of your login scripts (e.g. .bashrc or .bash_profile).

This statement confused me, because I don't know why that would be...

1 Like

I apologize, I would do the below in ubuntu 14.04

echo export PATH=$PATH:"/usr/bin/gatk-4.0.10.1" >> ~/.bashrc

to update .bashrc , although that doesn't work either. Thank you :).

though if I log out then login, it's fine.

so maybe, source ~/.bashrc after the command?

------ Post updated 10-17-18 at 07:15 AM ------

Thank you very much for your help :).

A few clarifications: you can change your environment (of which the PATH variable is a part) on the command line. Enter set or env at the command line to see what your current environment is. Basically, it is a set of declarations of variables in the form:

VARIABLE="some value"

and shell functions. Your PATH statement just modifies this value:

export PATH=$PATH:"/usr/bin/gatk-4.0.10.1"

means: set the variable PATH ("PATH=") to what it contains now ("$PATH"), plus a colon (":") and a fixed string ("/usr/bin/gatk-4.0.10.1").

Again, you can do that on the command line (like you did), but this will be lost when you log out. The file ~/.bashrc now contains such collected command line settings and it is executed every time you log in. That means, to make the change last you will have to modify this file, like you did:

echo export PATH=$PATH:"/usr/bin/gatk-4.0.10.1" >> ~/.bashrc

That simply adds (">>") the string export PATH=$PATH:"/usr/bin/gatk-4.0.10.1" to the end of the file ~/.bashrc - well, not quite, actually. Notice, though, that the content of "$PATH" will replace the string "$PATH" when you execute that and if, for instance, your PATH contained before the string:

/usr/bin:/usr/local/bin

then what you wrote into the file ~/.bashrc will be NOT the line

$ cat ~/.bashrc

<...old content of .bashrc...>
export PATH=$PATH:"/usr/bin/gatk-4.0.10.1"

but in fact:

$ cat ~/.bashrc

<...old content of .bashrc...>
export PATH=/usr/bin/usr/local/bin:/usr/bin/gatk-4.0.10.1

and this may or may not (most probably may not) be what you wanted. Anyway, when you want to have this setting not only in your future sessions but also in the current one you indeed have to use source ~/.bashrc because at the time when the file was executed - your login - the change was not there.

Further, appending directories to the path naively has a problem: you don't want the directories to be there several times. Take yours, for example, the existing path is just to show the effect, your will probably be different:

$ echo $PATH
/usr/bin:/usr/local/bin
$ export PATH=$PATH:"/usr/bin/gatk-4.0.10.1"
$ echo $PATH
/usr/bin:/usr/local/bin:/usr/bin/gatk-4.0.10.1
$ export PATH=$PATH:"/usr/bin/gatk-4.0.10.1"
$ echo $PATH
/usr/bin:/usr/local/bin:/usr/bin/gatk-4.0.10.1:/usr/bin/gatk-4.0.10.1

You probably want to avoid that. This is why i set the PATH in my rc-scripts this way:

export PATH="/usr/bin"               # here the existing PATH is overwritten
       PATH="$PATH:/usr/local/bin"
       PATH="$PATH:/some/other/dir"
       ...etc.

It is easy to add or remove a line with a single directory this way and it is easy to grasp immediately what is what. For not-so-obvious settings i use comments to remind me what it was for (so i can also easily find out if it can be removed again). Also notice that a variable is either exported or not. If it is there is no need to export it again, just because its value has changed.

You first try:

probably didn't work because by "sudo" you changed the environment of user "root", not the environment of the account you use to run the application. So, it "worked" in a way, but not one you could make use of.

I hope this helps.

bakunin