Skip to main content

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

Substring

$ STRING=string
# ${VARIABLE:START:LENGTH}
$ echo ${STRING:3}
ing
$ echo ${STRING:2:4}
ring