Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 1
    First of all, unquoted variable references are error-prone. That said, where does it say in any bash manpage that the [ or test built-in would test for file existence of the argument by default (as opposed to -e)? Would that not be ambiguous? AFAIK (and AIUI the section "CONDITIONAL EXPRESSIONS") the only thing that is tested with your approach is that the argument is not empty (or undefined), which is, in this case, a tautology (let $DIR = '' and $FILE = '', then the argument is still '//'). Commented Nov 9, 2012 at 2:59
  • 1
    Proof: ls /foo, result ls: cannot access /foo: No such file or directory. [ /foo ] && echo 42, result 42. GNU bash, version 4.2.37(1)-release (i486-pc-linux-gnu). Commented Nov 9, 2012 at 3:05
  • 1
    @PointedEars: I failed to specify the -f option, at the moment I wrote this answer. Obviously you could always use -e, if your not sure it will be a regular file. Additionally In all my scripts I quote these constructs, I must have just submitted this without adequate proofing. Commented Nov 17, 2012 at 23:42
  • ACK. But you probably know that a one-liner cannot solve the if-else problem: [ $condition ] && if_true || if_false is error-prone. In any event, I find [ ! -f "$file" ] && if_not_exists easier to read and understand than [ -f "$file" ] || if_not_exists. Commented Nov 18, 2012 at 1:33