1

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

1 Answer 1

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .