0

As far as I understand (feel free to correct me, I'm new to shell scripting), the bracket [ is a Unix command and also an alternative to the test command which allows you to test for a condition (returning 0 if condition is true and 1 otherwise)

Double brackets [[ is a keyword which allows you to do more complex condition evaluations. For example, the test against a regular expression.

If I understand correctly, double brackets [[ can potentially have the portability issues depending on a shell where the script is executed (for example, not supported by Bourne shell).

Therefore a question. If I want to test the variable for regular expression, is it possible to do using the test command (or the single bracket [ command)?

0

1 Answer 1

1

Traditionally test [ were different names (hardlinks) for one program, which did exactly the same thing(s) in both cases except for [ requiring the trailing ]; today they are usually shell builtins that still do the same except for ]. That program or those builtins do NOT do regular expressions.

The POSIX (and traditional) ways to test a regular expression are expr grep sed awk. The first does only few things including regexp, rather similar to test/[ and also [[. The second at core does only regexp, but with a lot of surrounding options. The last two do a lot of things including regexp, but if that's all you want you can do only that. Another common but not POSIX method is perl.

1
  • Thank you for the comment. Let me know if I understood this right. 1) I can't check for regex with test or [ ]. 2) The most common/traditional ways to do it is with expr grep sed awk or [[ ]] or perl Commented Nov 24, 2022 at 1:48

You must log in to answer this question.

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