Add directory to path permanently


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 10-17-2018
Quote:
Originally Posted by cmccabe
I apologize, I would do the below in ubuntu 14.04

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

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

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

so maybe, source ~/.bashrc after the command?
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:

Code:
VARIABLE="some value"

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

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

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

Code:
/usr/bin:/usr/local/bin

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

Code:
$ cat ~/.bashrc

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

but in fact:

Code:
$ 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:

Code:
$ 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:

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

Quote:
but when I do a sudo nano ~/.bashrc
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
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
NFS with a NAS: permanently inconsistent directory state across clients cosmojetz Solaris 10 05-12-2015 02:30 PM
Extract directory name from the full directory path in UNIX using shell scripting IshuGupta UNIX for Dummies Questions & Answers 5 06-03-2014 09:15 PM
Keep last directory from path Aswex Shell Programming and Scripting 1 11-12-2013 11:03 AM
How to get the current directory Path? Abhishek0683 UNIX for Dummies Questions & Answers 3 06-30-2013 02:14 PM
Can't add directory to path correctly tastybrownies Shell Programming and Scripting 2 02-09-2013 02:28 PM
Setting the path permanently using shell script pjeedu2247 Shell Programming and Scripting 6 01-22-2013 02:22 PM
How to select First two directory in from path name? yadavricky Shell Programming and Scripting 10 11-12-2012 12:33 PM
Add directory to a global PATH miltonrods UNIX for Dummies Questions & Answers 3 06-29-2012 11:38 AM
add path variable permanently burton Shell Programming and Scripting 4 06-15-2012 05:01 PM
"find . -printf" without prepended "." path? Getting path to current working directory? pstein Shell Programming and Scripting 1 10-02-2011 09:23 AM
Retrieve directory path from full file path through sh royzlife Shell Programming and Scripting 7 09-21-2010 09:28 AM
Is there any way to add an exception for a port in the firewall setting, permanently? senrooy IP Networking 1 03-02-2010 10:19 AM
Help with directory path parsing brianjbrady Shell Programming and Scripting 8 02-16-2010 01:58 PM
permanently add static route beaker457 Red Hat 1 04-17-2009 03:16 PM
How to get directory name from its path? ordano UNIX for Dummies Questions & Answers 1 02-12-2009 04:08 AM