Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I don't seem to understand how to "test" the conditional operators [[ and [. I tried using various form such as

echo [[a=a]]

[[a=a]]
echo $?

and some other things

I want to see what they return, to test some comparisons. How do you expand them, or execute them?

share|improve this question
    
Answers belong in the answer box below. –  Ignacio Vazquez-Abrams Dec 22 '11 at 5:21
    
I'll do that now –  rubixibuc Dec 22 '11 at 5:27
    
I hate posting an answer to my own question so I tried to leave it open –  rubixibuc Dec 22 '11 at 5:30
    
I suggest to think a little bit more next time, before posting such an elementary question. As you see, often answers come on their own. –  enzotib Dec 22 '11 at 6:55
    
@rubixibuc: Actually, answering your own question is something encouraged here (as unintuitive as it may seem). But I agree with enzotib, you did not really pick the most complex question to post ;) –  rahmu Dec 22 '11 at 6:57
add comment

2 Answers 2

up vote 1 down vote accepted

rubixibuc is right, spaces are necessary. You can test it with:

if [ "`whoami`" == "root" ]; then echo "To err is human...to really foul up requires the root password"; else echo "not telling any jokes"; fi
share|improve this answer
add comment

I didn't mean to post an answer right away but solved it after thinking about it more. There needs to a space between either the [[ or [ and the next token.

here's how you would test it.

[[ a = a ]]

or

[ a = a ]

then

echo $?
share|improve this answer
    
This is correct. [ and [[ are commands, and just as one must put a space after echo or ls, a space must follow these. –  Ignacio Vazquez-Abrams Dec 22 '11 at 5:34
2  
@IgnacioVazquez-Abrams Nitpick: [ is a command (and so if you don't put a space after it you get the [a=a] command), [[ is a keyword (but you still need the space after it, otherwise you get the [[a=a]] command). –  Gilles Dec 22 '11 at 8:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.