env PATH contains duplicate paths

env PATH contains duplicate paths

Post by Brian K. Quad » Wed, 04 Dec 2002 02:15:27



I am new to Linux and I use the RedHat Linux Netowrking and System
Administration book to find most of the answers I need.  But is says
that I should use .bashrc to add paths to my PATH environment variable.

Well, I did this and my path keep getting bigger with duplicate entries
of the same paths that I added.  The book does not tell me how to remove
anything from my path, only how to add it.

Here is what I have in my .bashrc file:
export JAVA_HOME=/usr/java/j2sdk1.4.1_01
export JWSDP_HOME=/root/jwsdp-1_0_01
export CATALINA_HOME=/Development/jakarta-tomcat-4.1.12
export ANT_HOME=/root/jwsdp-1_0_01
export
PATH=.:$PATH:$ANT_HOME/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin

And here is what my PATH looks like when I run env:
PATH=.:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11:/usr/local/bin:/usr/bin:/usr/java/j2sdk1.4.1_01/bin:/usr/X11R6/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin

How can I reset my path so that there are no duplicates and still have
it set all the necessary paths each time I bring up a new shell?

I am using RedHay 7.3.

 
 
 

env PATH contains duplicate paths

Post by Bit Twiste » Wed, 04 Dec 2002 02:19:09



> I am new to Linux and I use the RedHat Linux Netowrking and System
> Administration book to find most of the answers I need.  But is says
> that I should use .bashrc to add paths to my PATH environment variable.

> Well, I did this and my path keep getting bigger with duplicate entries
> of the same paths that I added.

Put it in ~./bash_profile

Global for everyone
        /etc/profile - environment variables (PATH, USER, LOGNAME,...)
        /etc/bashrc  - contains function & aliases, not environment vars

It is instructive to read the files in /etc/profile.d, if you have one.

I would place site/custom global environment variables in zz_local.sh
That way you can pop zz_local.sh in on new installs.

If you have an /etc/profile.d directory; do a

cd /etc/profile.d
touch zz_local.sh
chmod 755 zz_local.sh
Then add your changes, Example:  export PATH=$PATH:new_path:another_path

The zz_local.sh name was picked to force it to be executed last.
/etc/profile runs the scripts in /etc/profile.d
do a   ls -1 /etc/profile.d   to see order of file execution.

User only
        ~userid_here/.bash_profile - for environment variables
        ~userid_here/.bashrc       - for function & aliases, not env vars

ALWAYS do a    su -l user_id      to test your changes before logging out.

Profiles usually run once, bashrc run everytime you spin up a non-login
interactive session.

Sessions inherit env vars from the parent process.

Setting BASH_ENV=~/.bashrc will cause it to execute during
non-interactive session.

PS:
        for extra points do a
        man bash
        http://www.pathname.com/fhs/2.2/fhs-3.12.html

 
 
 

env PATH contains duplicate paths

Post by Chris F.A. Johnso » Wed, 04 Dec 2002 03:18:59



> I am new to Linux and I use the RedHat Linux Netowrking and System
> Administration book to find most of the answers I need.  But is says
> that I should use .bashrc to add paths to my PATH environment variable.

> Well, I did this and my path keep getting bigger with duplicate entries
> of the same paths that I added.  The book does not tell me how to remove
> anything from my path, only how to add it.

> Here is what I have in my .bashrc file:
> export JAVA_HOME=/usr/java/j2sdk1.4.1_01
> export JWSDP_HOME=/root/jwsdp-1_0_01
> export CATALINA_HOME=/Development/jakarta-tomcat-4.1.12
> export ANT_HOME=/root/jwsdp-1_0_01
> export
> PATH=.:$PATH:$ANT_HOME/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin

> And here is what my PATH looks like when I run env:
> PATH=.:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11:/usr/local/bin:/usr/bin:/usr/java/j2sdk1.4.1_01/bin:/usr/X11R6/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin:/root/jwsdp-1_0_01/bin:/usr/local/Acrobat5/bin:/usr/java/j2sdk1.4.1_01/bin

> How can I reset my path so that there are no duplicates and still have
> it set all the necessary paths each time I bring up a new shell?

    To clean your path of duplicates, and make sure that all
    directories it contains are valid you can use this function:

checkpath ()
{
    newPATH=
    local IFS=":"
    for p in ${PATH//\/\//\/}
    do
        if [ ! -d "$p" ]; then
            echo "checkpath: $p is not a directory; removing it" >&2
        else
            case :$newPATH: in
                *:$p:*)

                ;;
                *)
                    [ -d "$p" ] && newPATH=${newPATH:+$newPATH:}$p
                ;;
            esac
        fi
    done
    PATH=$newPATH
    unset newPATH
    return

Quote:}

    When adding directories to your path, check that they are not
    already there:

PATHLIST="$ANT_HOME/bin:/usr/local/Acrobat5/bin
/usr/java/j2sdk1.4.1_01/bin
/add/other/directories/here
"

for dir in $PATHLIST
do
  case :$PATH: in
      *:$dir:*) ;;
      *) [ -d $dir ] && PATH=${PATH:+$PATH:}$dir ;;
  esac
done

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

env PATH contains duplicate paths

Post by John Winte » Wed, 04 Dec 2002 21:18:41




[snip]

Quote:>        if [ ! -d "$p" ]; then
>            echo "checkpath: $p is not a directory; removing it" >&2

I'd suggest changing that message to something like:

            echo "checkpath: $p is not a directory; removing it from path" >&2

In its original form it could cause the odd moment of blind panic.

John
--
The Linux Emporium - the source for Linux in the UK
See http://www.linuxemporium.co.uk/

We had a woodhenge here once but it rotted.

 
 
 

env PATH contains duplicate paths

Post by Bruce Burhan » Thu, 05 Dec 2002 02:10:54





> [snip]
> >        if [ ! -d "$p" ]; then
> >            echo "checkpath: $p is not a directory; removing it" >&2

> I'd suggest changing that message to something like:

>             echo "checkpath: $p is not a directory; removing it from path"
>&2

> In its original form it could cause the odd moment of blind panic.

:-)  :-)

Bruce
--
     Bellingham   Washington   USA
     bburhan1  [ AT]   earthlink  [ DOT]  net

Quote:

> John
> --
> The Linux Emporium - the source for Linux in the UK
> See http://www.linuxemporium.co.uk/

> We had a woodhenge here once but it rotted.

 
 
 

env PATH contains duplicate paths

Post by Chris F.A. Johnso » Thu, 05 Dec 2002 03:26:47





> [snip]
>>        if [ ! -d "$p" ]; then
>>            echo "checkpath: $p is not a directory; removing it" >&2

> I'd suggest changing that message to something like:

>             echo "checkpath: $p is not a directory; removing it from path" >&2

> In its original form it could cause the odd moment of blind panic.

   :)
   Good point. Done.

   This gets posted quite frequently, as it's a F.A.Q.

   It'll be going on to my web site shortly, along with path, addpath
   and rmpath.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. add in the PATH a path if not present in the PATH

I look for a function in POSIX shell. this function must add path in the
$PATH if it isn't present.
by example:
I want add /bin in PATH
echo $PATH
--> /bin:/usr/bin
/bin is in the PATH then i don't add /bin
If I want add /usr/local/bin
/usr/local/bin isn't in the PATH then I add:
PATH=${PATH}:/usr/local/bin

  gaetan.schneller.vcf
< 1K Download

2. screen blanking issues.

3. ksh: add path to $PATH only when ot yet in $PATH

4. Need XF86Config help for S3 Trio32 on a DEC Venturis 400

5. zsh's 'typeset -U path' wipes out path/PATH

6. NFS performance data on AIX 4.2.1

7. PATH=$(getconf PATH), but PATH for getconf?

8. how do i install netscape

9. set path = "$path" hoses path in tcsh -- why???

10. How to expand paths in $PATH like "~/bin" to full path?

11. Relative path to Absolute path

12. tcsh PATH, path, and ~: bug or feature?

13. how to add a path to my path variable