什么是在[bash]中使用的[[-n variable]]语法

dps*_*dce 5 bash shell

我正在修复一些我经常看到的旧bash脚本

if [[ -n $VARIABLE ]]; then 
Run Code Online (Sandbox Code Playgroud)

语法我试图谷歌它,但可以找到为什么"-n"用于,以下是我所知道的

Comparisons:
  -eq   equal to
  -ne   not equal to
  -lt   less than
  -le   less than or equal to
  -gt   greater than
  -ge   greater than or equal to
Run Code Online (Sandbox Code Playgroud)

文件操作:

  -s    file exists and is not empty
  -f    file exists and is not a directory
  -d    directory exists
  -x    file is executable
  -w    file is writable
  -r    file is readable
Run Code Online (Sandbox Code Playgroud)

谁会让我知道-n做什么?

dev*_*ull 12

help test 会告诉你:

String operators:

  ....

  -n STRING
     STRING      True if string is not empty.
Run Code Online (Sandbox Code Playgroud)

  • @RaduRădeanu`if`命令采用`test`命令.它是`test`命令(或`[`),它有`-n`运算符. (2认同)