I try to use REGEX in bash in order to check if the parameter seems to be a valid path following this format:
/first/second
In java I use with success the following REGEX :
((\\/)[a-zA-Z0-9\\s_@\\-!#$%&+=]+)+
But in bash I have some issue that I can't explain. In order to simplify my comprehension I try to use a simpler REGEX like that:
CHECKPATH="!/first/second"
REGEX_PATH="((\/)[a-zA-Z0-9]+)+"
if [[ ! ${CHECKPATH} =~ $REGEX_PATH ]]; then
echo "error"
else
echo "OK"
fi
I insert an "!" in the path just to check. So I should have "error" but not. With or without the "!" it changes nothing, what's the problem?
Update: (moved author's subquestion here from a comment)
One more thing, if I use ^((\/)[a-zA-Z0-9\s_@#&%!$+=\-]+)+$ it's ok, but it's not with ^((\/)[a-zA-Z0-9\s_@\-#&%!$+=]+)+$. I just changed the position of "\-" inside the regex, why?
bash3.2 and and 4.3, with or with the!in the value ofCHECKPATH.