5

I only want to check in bash if a certain FILE exists.
On this page two options are mentioned to use with if:

  • [ -a FILE ] - True if FILE exists
  • [ -e FILE ] - True if FILE exists

Are they equivalent or is there any difference between them?

4
  • 4
    I think you can read link :stackoverflow.com/questions/321348/bash-if-a-vs-e-option, you can see difference between -a and -e.
    – Taneto
    Sep 5, 2017 at 10:43
  • Thanks @Tannetto so it seems "saver" to me using -e option in order to avoid the confusion with the -a meaning AND
    – derHugo
    Sep 5, 2017 at 10:46
  • @Tannetto nice find. You might want to reproduce a version of that answer here - (if you are copying, please use blockquote formatting and correct attribution)
    – Zanna
    Sep 5, 2017 at 10:46
  • @derHugo yes, it seems like -a is deprecated as its output may be unreliable
    – Zanna
    Sep 5, 2017 at 10:47

1 Answer 1

3

What does -a do and why does it exist ?

The -a option is the same thing as -e, and exists for compatibility with Korn Shell from which Bash borrowed a lot of features.

From POSIX standard, description of test command(link):

An early proposal used the KornShell -a primary (with the same meaning), but this was changed to -e because there were concerns about the high probability of humans confusing the -a primary with the -a binary operator.

side note: binary means flag that appears between two variables [ $var1 -a $var2 ], primary means appearing in the list of arguments first as in [ -a $var ]

In fact, Korn Shell (ksh93 here) manual states:

-a file

Same as -e below. This is obsolete.

The test command that is used in the TLDP article you referenced uses bash built-in test , however the option is also present in /usr/bin/test despite missing from documentation:

$ /usr/bin/test -a /etc/passwd  && echo 1                                   
1

If functionality is the same, is it good idea to use it?

So long as you are 100% sure your scripts will be used on either Bash or Korn Shell - then yes, it is alright. However, if you strive for portability of your scripts and want to write script the Right WayTM, you should use -e. The standard Ubuntu shell, /bin/sh, which is actually Dash - Debian Amquist Shell - doesn't recognize that as valid option:

$ dash
$ test -a /etc/passwd
dash: 1: test: -a: unexpected operator

In cases where you want to port your script to other platforms, using -a is not guaranteed to be safe with /usr/bin/test as well, and you should stick with -e because it is in fact specified by POSIX standard.

1
  • 1
    Thanks for sharing this background knowledge. Though my question was actually already solved by @Tannetto I accepted your answer as solution since it explains well in an understandable way why it's better to use -e ;)
    – derHugo
    Oct 28, 2017 at 16:50

Your Answer

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

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