env PATH contains duplicate paths 

Newsgroups: comp.os.linux.misc
Date: Mon, 02 Dec 2002 17:19:09 GMT
> 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.

Bit Twister

env PATH contains duplicate paths 

> 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 path" >&2
        else
            case :$newPATH: in
                *:$p:*)

                ;;
                *)
                    [ -d "$p" ] && newPATH=${newPATH:+$newPATH:}$p
                ;;
            esac
        fi
    done
    PATH=$newPATH
    unset newPATH
    return
}
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