[Bash] [ 实际上是一个命令

1  采用 [ 时,有四个地方必须有空格,即 if [ -f "$FILE" ];then echo exists; fibash

2 采用test命令时,有三个地方必须有空格,即 if test -f "$FILE";then echo exists; fispa

#!/bin/bash
#test file exists

FILE="1"
if [ -e "$FILE" ];then echo exists; fi

if test -e "$FILE";then echo exists; fi


if [ -e "$FILE" ] ; then echo exists; fi

if [ -e "$FILE" ]; then echo exists; fi

if [ -e "$FILE" ]; then echo exists; fi

[ -e "$FILE" ] && echo exists

test -e "$FILE" && echo exists

以上写法都是合法的。code

你之因此须要三个或四个空格,是由于[本质上是一个Linuxe命令。it

The reason you need a space is because [ is actually a command line. Type which [ and you will see what that it is in /usr/bin. You can write any if [...];then command as if test ...;thenclass

相关文章
相关标签/搜索