Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
keep duplicates out of $PATH on source

I am not an expert with Unix scripting. Some of my scripts are located in $HOME/mydir. Unfortunately, they are not accessible from elsewhere if this dir is not part of PATH.

I would like to create a small scripts checking whether $HOME/mydir is part of PATH. If it is not, PATH should be updated with it and EXPORTED.

My question is: which command can I use to check whether $HOME/mydir is part of PATH? Thanks.


For the records, I implemented the following:

echo "Before: $PATH"

echo $PATH | grep -q "$HOME/scripts"

if [ $? -eq 0 ]; then
  export PATH
  export PATH

echo ""
echo "After: $PATH"
share|improve this question

marked as duplicate by Mat, manatwork, jw013, Gilles, Michael Mrozek Feb 19 '12 at 7:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

are you sure this works? I made a typo in my response, which I have now edited. The exit code is in the $? variable, not $0, which should be the name of your script. Change $0 == 0 to $? -eq 0. You should try it with a some path that is not in your PATH – Rado Feb 18 '12 at 18:33
It worked on my limited tests, but I have updated my code. – JVerstry Feb 19 '12 at 9:43
up vote 2 down vote accepted

You can write a simple bash script to do something like this.

echo $PATH | grep -q  "/your/search/path"

Then check if $? is not 0, meaning no match, and if so add the path

export PATH=$PATH:/your/search/path
share|improve this answer
Thanks, it does work !!! – JVerstry Feb 18 '12 at 15:39
$0 ?? is it right way to do? – Nikhil Mulley Feb 18 '12 at 17:47
@NikhilMulley, no it's not ... it's a typo, should be $?. – Rado Feb 18 '12 at 18:29
glennjackman's answer is the more correct way to do this. – jw013 Feb 18 '12 at 18:54
@jw013: there is no correct, on incorrect way to do it, there is ways that work and ways that do not, and I think the grep way works. Maybe you meant that using grep in this case waste a process, but I think it is very clean and easy to understand, so it is fine if performance is not an issue. Maybe you also meant that it might break if a subdirectory of /your/search/path is in your path, but it can easily be fixed (thanks to glennjackman idea of :). It can even be simplified as: grep -q ":/your/search/path:" <<< ":$PATH:" || export PATH=$PATH:/your/search/path. – jfgagne Feb 19 '12 at 13:26

I would write

case ":$PATH:" in
  *:$HOME/mydir:*) echo it is in the path;;
  *) echo not there ;;

I write ":$PATH:" to ensure that the pattern matches if the desired path is either first or last in your $PATH.

share|improve this answer
right way to do things with $PATH stuff. – Nikhil Mulley Feb 18 '12 at 17:47
Great one on :$PATH: as it also make sure that none of $HOME/mydir/subdir or $HOME/mydirectroy is in the path. +1. – jfgagne Feb 19 '12 at 13:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.