function::matched - Linux


The function::matched command checks if an input string matches a specified regular expression pattern.


function::matched PATTERN STRING [FLAGS]


| Parameter | Description |
| PATTERN | The regular expression pattern to check against. |
| STRING | The input string to match. |
| FLAGS | Optional flags to modify the search behavior. |


| Flag | Description |
| -i | Case-insensitive matching. |
| -s | Single-line matching. |
| -m | Multi-line matching. |
| -x | Exact match. |


Simple matching:

function::matched "foo" "foobar" # Returns True

Case-insensitive matching:

function::matched -i "FOO" "foobar" # Returns True

Multi-line matching:

function::matched -m "foo\nbar" "foobar\nhello" # Returns True

Common Issues

  • Invalid regular expression: Ensure that the pattern is a valid regular expression. Check for syntax errors and proper escaping.
  • No match found: Verify that the input string actually contains the pattern you’re searching for.


function::matched can be used in scripts and pipelines to conditionally run commands or process data based on matching criteria:

if function::matched "error" "$log"; then
  echo "An error occurred!"

Related Commands

  • grep: Find patterns in text files.
  • sed: Perform text transformations based on patterns.
  • awk: Manipulate and analyze text data.