There are several problems going on here.
First, you need to include a space between the operator and the expression. This is what's giving you the "unary operator" error.
is the old-style traditional test operator, which only does literal string matching. It cannot match substrings. You have to use the newer [[
test instead for that (or use a different type of evaluation entirely, such as a case statement*).
Third, inside [[
, the "==" operator applies either a literal or a globbing pattern match, both of which must still must match the string as a whole
, not a substring. Add "*
" globs to either end, and do not
quote it (or else it will be treated as a literal string to match).
So to test for the existence of a space or tab character inside the variable temp
if [[ $temp == *[:blank:]* ]]; then
also allows full regular expression matching, using the =~
operator. Regex allows you to match substrings. Cedrik's version above almost does it, but he made one small mistake. The [:blank:]
character class now has to be enclosed in a regex "
" character range expression. So:
if [[ $temp =~ [[:blank:]] ]]; then
Also, particularly for complex pattern matches, you should store the expression in a separate variable first. Otherwise you may have problems trying to escape everything properly
if [[ $temp =~ $re ]]: then
* (Note) A case statement can be used to apply globbing patterns to variables even in shells that don't support "[[
case $temp in
*[:blank:]*) <commands> ;;