How to match string using regular expression in bats tests?

1k Views Asked by At

I know I can check if the result is equal to something, like

#!/usr/bin/env bats

@test "Check that total is listed" {
    run ls -l
    [[ ${lines[0]} =~ "total" ]]
}

but, what if I just want to check if the lines[0] is made of several digits and letters? Should I use a regular expression to do that?

1

There are 1 best solutions below

0
On

Use a regex, example:

$ regex='^[[:alnum:]]+$'
$
$ [[ "1gz0" =~ $regex ]]
$ echo $?
0
$ [[ "1gz_0" =~ $regex ]]
$ echo $?
1