{"id":686,"date":"2020-04-07T19:38:35","date_gmt":"2020-04-08T00:38:35","guid":{"rendered":"http:\/\/csic.som.emory.edu\/~lzhou\/blogs\/?p=686"},"modified":"2020-04-07T19:38:35","modified_gmt":"2020-04-08T00:38:35","slug":"avoid-duplicates-in-path","status":"publish","type":"post","link":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/?p=686","title":{"rendered":"Avoid duplicates in $PATH"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Linux search \/etc\/profile.d and ~\/.bashrc, ~\/.bash_profile for system and user defined environmental variables.  One of the most manipulated variable is $PATH.  You can see this by running<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$ echo $PATH<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In most cases, when a new 3rd party software is installed, and you want it to be in your default executable search path, you insert a line like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PATH=$PATH:\/where\/your\/new_executables<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">in your ~\/.bash_profile or in a file in \/etc\/profile.d if you are a system administrator.  This appends a new search path onto the existing one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if you launch another terminal, a shell, etc., all these scripts are loaded again and you got duplicated entries in $PATH.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This looks harmless and does not affect system performance in most cases since bash looks for the first available executable in the search path and ignore all later appended paths.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if you are running some scripts that loop into multiple layers of shells, this could cause the $PATH string overflow and disrupt your script.  Also for cosmetic reason, a very long $PATH filled with duplicates are ugly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To not generate duplicates, we can do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NEWPATH=\/where\/your\/new_executables \nif [ -d \"$NEWPATH\" ] &amp;&amp; [[ \":$PATH:\" != *\":$NEWPATH:\"* ]]; then\n    PATH=\"${PATH:+\"$PATH:\"}$NEWPATH\"\nfi\nexport PATH;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">for appending or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NEWPATH=\/where\/your\/new_executables \nif [ -d \"$NEWPATH\" ] &amp;&amp; [[ \":$PATH:\" != *\":$NEWPATH:\"* ]]; then\n    PATH=\"$NEWPATH${PATH:+\":$PATH\"}\"\nfi\nexport PATH;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">for prepending, instead of the <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PATH=$PATH:\/where\/your\/new_executables<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">way.  This, will prevent generating duplicated entries in $PATH since it will only add in a new entry when the new entry is not found in the existing environmental variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux search \/etc\/profile.d and ~\/.bashrc, ~\/.bash_profile for system and user defined environmental variables. One of the most manipulated variable is $PATH. You can see this by running $ echo $PATH In most cases, when a new 3rd party software is installed, and you want it to be in your default executable search path, you insert [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,3],"tags":[],"class_list":["post-686","post","type-post","status-publish","format-standard","hentry","category-computer-tips","category-mri-technical-support","post-blog"],"_links":{"self":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=686"}],"version-history":[{"count":2,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/686\/revisions"}],"predecessor-version":[{"id":688,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/686\/revisions\/688"}],"wp:attachment":[{"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csic.som.emory.edu\/~lzhou\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}