Directories in the
$PATH variable are separated by
:. So you can use a regular expression to test whether the
$PATH contains a directory.
The code for your specific example will be as follows (print true or false).
if [[ "$PATH" =~ (^|:)"/usr/local/mytool/bin"(|/)(:|$) ]]; then
There are several corner cases handled:
The path is the first directory in
(^|:) at the beginning of the regex.
The path is the last directory in
(:|$) at the end of the regex.
The path can have a trailing '/' or not. So
(|/) after the path.
Of course, there are still other wired cases. But they are not handled here.