Geek‎ > ‎

BASH wizardry

Just flinging stuff in here for now:

Variable Regex:

Has has a regular expression operator `=~' . Every substring matched by the subexpression can be accessed from the BASH_REMATCH variable. Example:
#!/bin/bash
VAR="abc_12345_whatever"
if [ "$VAR" =~ [[:alpha:]]_[[:digit:]]_[[:alpha:]] ]; then
  echo "${BASH_REMATCH[2]}"
NOTE: The comparison string on the right cannot be quoted! If it is, it's taken as a string, so the match won't be made!
Variable expansion:

Comments