Skip to contentSkip to conversation

Adding to the PATH but not if already in $PATH

DISCUSSION Dog Walker 2 participants 2 messages Feb 26, 2005

lore
Share & tools
lore

Dog Walker

author
#0 lore raw

I want to prepend a directory to my PATH in my $HOME/.bashrc and export the result. But I only want it to happen once: iow, if a directory I want prepended is already in the PATH, do not prepend it again.

Something like:

if "/home/dw/bin" not in $PATH ; then
PATH=/home/dw:$PATH
export PATH
fi

The questions are: Is this the way one guards against multiple additions in subshells? Is there a way to check for a substring?

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw
Quoted 10 lines · Dog Walker

I want to prepend a directory to my PATH in my $HOME/.bashrc and export the result. But I only want it to happen once: iow, if a directory I want prepended is already in the PATH, do not prepend it again.

Something like:

if "/home/dw/bin" not in $PATH ; then
PATH=/home/dw:$PATH
export PATH
fi

The questions are: Is this the way one guards against multiple additions in subshells? Is there a way to check for a substring?

Hi, DW:

-----
#!/bin/sh
#

# file /usr/local/bin/phwb.sh
#
# This prepends "/home/dw/bin/" to $PATH
PATH=/home/dw/bin:$PATH
export PATH
#end
-----
Quoted 3 lines

touch /home/dw/bin/phwb.sh
chmod +x /home/dw/bin/phwb.sh
phwb.sh

If there is already a path to /home/dw/bin, then the 0 byte file /home/bin/dw/phwb is executed.
else: /usr/local/bin/phwb.sh is executed.

HTH, Chuck

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs