0

I would like to check if two arguments to a bash function are numeric, and the last argument is a directory that exists.

Have done like this, but the implementation is quite insufficient.

if   [[ $1 =~ "^[0-9]+$" ]]  \
  && [[ $2 =~ "^[0-9]+$" ]]  \
  && [[ -d $3 ]]
then
  do_something
fi
0
0

Regex metacharacters should not be quoted – "^[0-9]+$" tries to match ^[0-9]+$ as a string.

man bash says

An additional binary operator, =~, is available ...

... Any part of the pattern may be quoted to force the quoted portion to be matched as a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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