What's Linux test -a command test for?

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, My system is Ubuntu 13.10 and my man test says GNU coreutils 8.20 October 2012 TEST(1).

3 Answers

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.

2

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).

-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.

Reference (scroll down some after info coreutils 'test invocation'):

16.3.6 Connectives for `test'

The usual logical connectives.

`! EXPR' True if EXPR is false.

`EXPR1 -a EXPR2' True if both EXPR1 and EXPR2 are true.

`EXPR1 -o EXPR2' True if either EXPR1 or EXPR2 is true.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like