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.

In the if [ -e $name ], what condition check is -e?
$name is a variable of a path to a directory.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

-e returns true if the target exists. Doesn't matter if it's a file, pipe, special device, whatever. The only condition where something may exist, and -e will return false is in the case of a broken symlink.

For example:

$ ln -s foo bar

$ [ -e foo ]; echo $?
1

$ touch bar

$ [ -e foo ]; echo $?
0

In bash you can do help test to see what test options you have.


[ is usually part of your shell. In bash the options and behaviors are defined by bash. It is also kind of a synonym for test. In bash you can do help test to see all the options it supports.
The only real difference between [ and test should be that [ requires a ] after your arguments, whereas test does not. They otherwise work the same, [ -e foo ] is equivalent to test -e foo.

There is also /usr/bin/[ for shells which do not have [ built in. There is no man page for this though. But there is also a /usr/bin/test, and my system does have a man test which covers the options. I haven't tested, but I'd bet all the options supported by /usr/bin/test work on /usr/bin/[.

share|improve this answer
    
[ and test are indeed the same program (and the POSIX spec requires them to be). Someone in the dim recesses of time thought that providing a synonym for test--right down to the required but unused ] argument--was a good idea, but it's just been a source of confusion for countless beginners who think the [ and ] really are shell syntax. –  chepner May 6 at 15:54

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.