13

Possible Duplicate:
keep duplicates out of $PATH on source

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.

SOLUTION

For the records, I implemented the following:

echo "Before: $PATH"

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

if [ $? -eq 0 ]; then
  PATH=$PATH:$HOME/scripts
  export PATH
else
  export PATH
fi

echo ""
echo "After: $PATH"
2
  • 1
    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, 2012 at 18:33
  • It worked on my limited tests, but I have updated my code. Feb 19, 2012 at 9:43

2 Answers 2

25

I would write

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

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

3
  • right way to do things with $PATH stuff. Feb 18, 2012 at 17:47
  • 1
    Great one on :$PATH: as it also make sure that none of $HOME/mydir/subdir or $HOME/mydirectroy is in the path. +1.
    – jfg956
    Feb 19, 2012 at 13:28
  • That’s short, readable, clever – and POSIX compatible to boot! Oct 4, 2016 at 12:15
9

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
6
  • $0 ?? is it right way to do? Feb 18, 2012 at 17:47
  • @NikhilMulley, no it's not ... it's a typo, should be $?.
    – Rado
    Feb 18, 2012 at 18:29
  • glennjackman's answer is the more correct way to do this.
    – jw013
    Feb 18, 2012 at 18:54
  • 3
    @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.
    – jfg956
    Feb 19, 2012 at 13:26
  • @jfgagne There are certainly better and worse ways to do things. The case statement is a shell built-in - it's better for at least two reasons: (1) no external dependencies and (2) it doesn't need to fork a new process (slightly better performance). So far you haven't proved your point as you've only pointed out that using grep can get similar results, but you have not mentioned any advantages of using grep over case.
    – jw013
    Feb 19, 2012 at 15:52

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