bash - How can I grep complex strings in variables? -
i trying grep small string in larger string. both strings being stored variables , here code example:
#!/bin/bash long_str=$(man man) shrt_str="guide" if grep -q $shrt_str $long_str ; echo "found it!" fi
i don't think variable expansion working way expect to. have tried [ ]
, [[ ]]
, quoting variables , piping output /dev/null
no matter won't work.
does have ideas?
echo "$long_str" | grep -q "$shrt_str" if [ $? -eq 0 ];then echo "found" fi
or
echo "$long_str" | grep -q "$shrt_str" && echo "found" || echo "not found"
but since using bash shell, use shell internals. no need call external commands
shrt_str="guide" case "$long_str" in *"$shrt_str"* ) echo "found";; * ) echo "not found";; esac
Comments
Post a Comment