String
Match
$ FOO=foo
# use regular expression
$ [[ $FOO =~ ^f ]] && echo matched || echo unmatched
matched
$ [[ $BAR =~ ^f ]] && echo matched || echo unmatched
unmatched
# use glob
$ [[ $FOO == f* ]] && echo matched || echo unmatched
matched
$ [[ $BAR == f* ]] && echo matched || echo unmatched
unmatched
- Reference: https://stackoverflow.com/questions/17420994/how-can-i-match-a-string-with-a-regex-in-bash
Substring
$ STRING=string
# ${VARIABLE:START:LENGTH}
$ echo ${STRING:3}
ing
$ echo ${STRING:2:4}
ring