开夏 | if 语句中的 `-n` 选项

KSH | `-n` option inside an if statement

我在检查 korn shell 脚本时偶然发现了这个:

if [[ -n `echo "This is an example." | grep -E "example"` ]]; then
  # Do something.
fi

谁能告诉我 -n 在做什么?

建议检查这个 article:

 [[ -n "string" ]] true if length of string is greater than zero

类似于

if [[ `echo "This is an example." | grep -Ec "example"` -gt 0 ]]; then
  # Do something.
fi

if 语句的更好实践:

if [[ -n $(echo "This is an example." | grep -E "example") ]]; then
  # Do something.
fi