Source From Here
Question
As title. How to use regex to compare string in Bash?
How-To
To match regexes you need to use the =~ operator. For example:
- testReg.sh
Execution sample:
Alternatively, you can use wildcards (instead of regexes) with the == operator:
Execution result:
Question
As title. How to use regex to compare string in Bash?
How-To
To match regexes you need to use the =~ operator. For example:
- testReg.sh
- #!/bin/bash
- IFS='\n' read -a array <<< `find ~/ -type f -name "*.sh"`
- for element in ${array[@]}
- do
- [[ $element =~ test.* ]] && echo "$element (o)" || echo "$element (x)"
- done
Alternatively, you can use wildcards (instead of regexes) with the == operator:
- #!/bin/bash
- IFS='\n' read -a array <<< `find ~/ -type f -name "*.sh"`
- for element in ${array[@]}
- do
- [[ $element == *test* ]] && echo "$element (o)" || echo "$element (x)"
- done
沒有留言:
張貼留言