bash脚本,调用函数作为条件测试

Xia*_*Guo 3 bash scripting

有人可以告诉我为什么第7行(if语句)会产生错误:

test.sh: line 7: [: command_exists: unary operator expected
Run Code Online (Sandbox Code Playgroud)

谢谢!

#!/usr/bin/env bash

command_exists () {
  command -v "$1" &> /dev/null ;
}

if [ ! command_exists ruby ]; then  # test.sh: line 7: [: command_exists: unary operator expected
  echo 'found ruby'
else
  echo 'ruby not found'
fi
Run Code Online (Sandbox Code Playgroud)

Pau*_*ce. 5

if ! command_exists ruby; then
Run Code Online (Sandbox Code Playgroud)

在Bash中,if执行命令并对其返回值执行操作.[碰巧是一个命令.