cmdcmd
1
str contains tabs and multiple spaces
str="hello world. How are you?"
I want to check string start with hello world,
and my code is:
if [[ $str == "hello[[:blank:]]world"* ]]; then
echo "found"
else
echo "not found"
fi
Not work
Other solution may work is to replace all tabs and spaces with a single space. I googling and found a solution
shopt -s extglob
temp=$(echo "${str//+([[:blank:]])/ }")
if [[ $temp == "hello world"* ]]; then
echo "found"
fi
But I don't want to replace, just check in condition. I am new to shell, sorry.
Hi, maybe this will help?
if [[ $(echo $str) == "hello world"* ]];
3 Likes
That's clever. By feeding it into a command without quotes, you've flattened the whitespace.
2 Likes
RudiC
4
You seem to have a sufficiently recent shell... man bash
:
Thus, try also
if [[ $str =~ "^hello[[:blank:]]*world" ]];
1 Like
Then already
if [[ "$str" =~ ^"hello"[[:blank:]]+"world" ]];
--- Post updated at 23:57 ---
or
if [[ "$str" =~ ^hello[[:blank:]]+world ]];
--- Post updated 03-21-19 at 00:04 ---
Actually this is news to me. It's regexp. Even have to escape question mark
if [[ "$str" =~ ^hello[[:blank:]]+world.*\?$ ]];