14

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"
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 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 '12 at 18:33
  • It worked on my limited tests, but I have updated my code. – Jérôme Verstrynge Feb 19 '12 at 09:43

2 Answers2

27

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.

glenn jackman
  • 85,964
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
Rado
  • 256
  • $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
  • 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 '12 at 13:26
  • 1
    @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 '12 at 15:52
  • @jw013: I think the grep solution is more readable. Moreover, it fits on one line. I would not use it to add 10.000 elements to my PATH, but for a single addition, it fits most needs. ;-) – jfg956 Feb 19 '12 at 17:56