Bash if statement can be used with regular expression
if [[ "hello world" =~ "hello" ]] ## this is true
then
echo "'hello world' has 'hello'"
fi
if [[ "hello" =~ "hello world" ]] ## this is false
then
echo "'hello' has 'hello world'"
fi
if [[ "hello world" =~ e.* ]] ## this is true
then
echo "'hello world' has 'e' followed by any character"
fi
Note that
- the bracket should be double
- use "=~", not "~="
- use the regular expression without quotation mark
- the lefthand-side should be a container
'Programming > Bash Script' 카테고리의 다른 글
Bash script argument parsing with getopts (0) | 2020.01.23 |
---|---|
Bash script for statement (0) | 2018.05.21 |
Bash Script case statement (0) | 2018.02.06 |
Bash Script Variable Split by Space (0) | 2018.02.04 |
Bash Script Echo (0) | 2018.02.04 |