My answer over at StackOverflow, which received at least two upvotes, is what worked on the shell of a workstation I used at my previous workplace. On that workstation, which is probably some old version of SunOS or Red Hat Commercial Linux or Red Hat Enterprise Linux, [[ -fwdpath]] tests the existence of path as both a regular file and a writable directory. sh, bash, csh, tcsh and zsh were the only possible shells installed on that workstation, and I most likely ran it in bash.

I'm asking this question because I was not able to reproduce that now on other Unix-like operating systems. It always gives me a syntax error, so I'm starting to doubt if I ever ran it successfully.

My question is: on what systems is this code valid, and if so, is this a bug or a feature?

  • Which bash version do you use on SunOS and RHEL?
    – Cyrus
    Dec 26 '17 at 22:26
  • Not valid on bash version 4.3.30(1)-release (i386-pc-solaris2.11). I get syntax error as well if I try multiple file test operators.
    – jesse_b
    Dec 26 '17 at 22:48
  • @Cyrus No clue, but if it's SunOS, it's either one of the SunOS 4.x versions or SunOS 5.10, so probably bash 1 or 2, and maybe installed in the xpg4 directory?
    – Gao
    Dec 27 '17 at 1:43
  • @Cyrus I remember seeing a ucb directory on some of the workstations, too, so bash could also be installed there, or just plain old /bin or /usr/bin. It might also have been SunOS 5.11... Sorry my memory is hazy...
    – Gao
    Dec 27 '17 at 1:57

No, that doesn't work. Not really in any version of bash.

This test works in any bash version (in fact, in any shell):

$ [ -fwd ] && echo "It worked" || echo "not"
It worked

But what it is testing is not a file flag, it is testing whether the string -fwd is of length 0 or not. As it has some characters, the test is true.

Using [[ change which flags are allowed:

$ [[ -fwd  ]] && echo "It worked" || echo "not"
b204sh          : /home/user/so: line 3: unexpected argument to conditional unary operator

Gives the above error in bash up to bash 3.2.
There is no error for bash versions after 3.2, but the result is truthful:

$ [[ -fwd ]] && echo "It worked" || echo "not"
It worked

which is imposible: a directory entry can not be both a file (-f) and a directory (-d).

This confirms the idea that the test is actually testing the string:

$ [[ \-fwd ]] && echo "It worked" || echo "not"
It worked

Testing with a file

$ touch wds
$ [[ -fwd wds ]] && echo "It worked" || echo "not"
bash: conditional binary operator expected
bash: syntax error near `wds'

Gives a fail message on bash versions after 3.2.
For other versions it gives a truthful answer (which is imposible for file tests, a directory entry could not be at the same time a file and a directory).

Comparing this two tests, it seems that only the first letter is used:

$ b204sh -c '[[ -fd wds ]] && echo "It worked" || echo "not"'
It worked

$ b204sh -c '[[ -df wds ]] && echo "It worked" || echo "not"'

Likely a bug corrected on bash 4

Fails also in:

[[: No match.
[[: Command not found.
unknown condition: -fd # Version 5.4.2
syntax error at line 3:wds' unexpected` # Version AJM 93u+ 2012-08-01

  • You're saying [[ -fwd wds ]] gives a truthful answer on bash versions prior to version 3.2?
    – Gao
    Dec 27 '17 at 2:08
  • @Gao Yes, in bash 2.04, 2.05, 3.0 and 3.2 (the versions tested).
    – ImHere
    Dec 27 '17 at 2:48
  • Could you please try [[ -rwx file ]] and observe the return status for each bit mask (1, 2, 4 is enough)? I vaguely remember (though I could be wrong) that it's not just the first letter that is used. I also think it's a bug because shopt -s compat31 # or compat32 on bash 4.4.12(1)-release doesn't reproduce this behavior.
    – Gao
    Dec 27 '17 at 4:09
  • @Gao b204sh -c '[[ -rx wds ]] ; echo "$?"' outputs 0, however b204sh -c '[[ -xr wds ]] ; echo "$?"' outputs 1. That is a clear conflict with the theory that all flags are tested. That doesn't work.
    – ImHere
    Dec 27 '17 at 4:39

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.