bef*_*ire 4 testing shell posix file
我在这个表达式中对bash感到困惑:
$ var="" # empty var
$ test -f $var; echo $? # test if such file exists
0 # and this file exists, amazing!
$ test -f ""; echo $? # let's try doing it without var
1 # and all ok
Run Code Online (Sandbox Code Playgroud)
我无法理解这种bash行为,也许有人可以解释一下吗?
这是因为$var在test看到它之前,空的扩展被删除了.你实际上在运行test -f,因此只有一个arg test,即-f.根据POSIX,一个类似的arg -f是真的,因为它不是空的.
1 argument:
Exit true (0) if `$1` is not null; otherwise, exit false.
Run Code Online (Sandbox Code Playgroud)
永远不会测试具有空文件名的文件.现在有一个显式test -f ""有两个args并被-f认为是"测试存在路径参数"的运算符.
| 归档时间: |
|
| 查看次数: |
144 次 |
| 最近记录: |