Here is one way to make it more portable and robust:
add2path() {
if ! echo "$PATH" | PATH=$(getconf PATH) grep -Eq '(^|:)'"${1:?missing argument}"'(:|\$)' ; then # Use the POSIX grep implementation
if [ -d "$1" ]; then # Don't add a non existing directory to the PATH
if [ "$2" = front ]; then # Use a standard shell test
PATH="$1:$PATH"
else
PATH="$PATH:$1"
fi
export PATH
fi
fi
}
I have added a test to quit the function with an error should no argument be passed.
PATH=$(getconf PATH) is the portable way to have the POSIX commands first in the PATH. This is solving the issue you had with Solaris where, not to break existing Solaris scripts portability, the first grep command found in the PATH is not POSIX compliant.
Using [[ is not specified by POSIX so it is better to stick with [ to do tests.
$2 should then be quoted to avoid clashes with [ operands.
front, being a constant string needs, need not be quoted.
Excluding non directories to be added looks to me a reasonable approach but is of course an optional extension which you might remove.