Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Please take a look at the following code,

snap=snapshot.file   
touch snapshot.file-1

$ [ -a $snap-1 ] && echo yes 
yes

What does the test -a command tests for here?

I tried info coreutils 'test invocation' and searched for -a, but didn't find it in the file characteristic tests section, but rather in the connectives for test section.

Is such test -a command an undocumented one?

NB,

  • Before jump to any conclusion, please make sure you can duplicate the about result first.
  • My system is Ubuntu 13.10 and my man test says GNU coreutils 8.20 October 2012 TEST(1).
share|improve this question

3 Answers 3

up vote 2 down vote accepted

I am not sure why the info page doesn't have it, but running help test in bash gives the answer:

...
    File operators:

  -a FILE        True if file exists.
...

So it is simply an "existence" test, no other permissions/attributes checked.

share|improve this answer
    
Perfect! Thanks. Strange, man test doesn't have that either. only help test does. Does that mean it is a bash specific file existence testing? –  xpt Jun 17 at 3:57
1  
@xpt It would seem like that is the case, but for me /bin/test -a file worked also...So why it isn't in the manpage I have NO idea. –  BenjiWiebe Jun 17 at 12:13

If you're running test or [ in bash, it's actually probably the built-in version, and not the coreutils version in /usr/bin:

$ type test
test is a shell builtin
$ type [
[ is a shell builtin

That said, it does appear that the coreutils version implements both -a and -e, with exactly the same behavior. Maybe -a is not reflected in the manpage because it's not standard, so maybe it was added later and that person neglected to update the manpage accordingly. But I can't say I know the history behind why it was added (or even what the a is supposed to be short for).

share|improve this answer

-a is the AND operator for combining conditions, so the following shows yes if both directories exist:

[ -e /root -a -e /usr ] && echo yes

The use of it with a single condition that defaults to an existance test seems like retained old behavior but I can't find it in old man pages.

share|improve this answer
1  
If you look at the coreutils source, the a and e cases share the same code in the function that handles unary operators, so I don't think it's an accidental thing. –  jjlin Jun 17 at 17:33

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.