0

Is there any difference between the following if statements in bash?

if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then 

Which should I prefer? Are any of those "better"?

2
  • The first one seems clearer for sure Commented May 16, 2013 at 13:28
  • 1
    Note that if is irrelevant for the question, you can use [ even without it. Commented May 16, 2013 at 13:31

2 Answers 2

3

The -a isn't available in all older (pre-POSIX) shells, so you should prefer && when trying to be portable.

That said, if you're ONLY supporting bash, use its built-in [[ ]] facility, which allows && within a test rather than only outside it (and, as an added bonus, makes quoting optional):

[[ -z $debian_chroot && -r /etc/debian_chroot ]] 

See also http://mywiki.wooledge.org/BashGuide/TestsAndConditionals

Sign up to request clarification or add additional context in comments.

Comments

3

The first is preferred. See the "Application Usage" section of the POSIX standard for the test built-in for the reason. In brief, -a is an extension marked as obsolete due to ambiguities in meaning for certain expressions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.