How to check for blank spaces in a string?
my code for checking blank space in a string is as follows
Code:
if [ $temp ==[:blank:] ] but i am getting error as [: ==: unary operator expected how i can rectify this |
Use double quotes like this
Code:
if [ "$temp" == "[:blank:]" ] |
but still i m getting error as
[: =: unary operator expected |
Code:
if [[ $temp =~ [:blank:] ]] |
when i tried with blank
i got ans 5 times but in my string there is only one space |
WHEN I triede with space its working but even if there is only one space many times "hi" gets displayed
|
Quote:
|
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. Second, [ 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: Code:
if [[ $temp == *[:blank:]* ]]; then Code:
if [[ $temp =~ [[:blank:]] ]]; then Code:
re='[[:blank:]]' * (Note) A case statement can be used to apply globbing patterns to variables even in shells that don't support "[[". Code:
case $temp in |
Quote:
|
All times are GMT -5. The time now is 04:03 PM. |