[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

相關文章
相關標籤/搜索