1.test,/usr/bin/test,[],和/usr/bin/[介绍

首先,linux下test,/usr/bin/test,[],和/usr/bin/[命令都是等价命令

其次,linux下test,/usr/bin/test,[],和/usr/bin/[命令一般用来测试文件类型,或者用来比较字符串

test,/usr/bin/test,[],和/usr/bin/[命令的区别:

1.test是shell内建命令

[root@elasticsearch ~]# type test
test is a shell builtin
[root@elasticsearch ~]#

2./usr/bin/test是外部命令

[root@elasticsearch ~]# type /usr/bin/test
/usr/bin/test is /usr/bin/test
[root@elasticsearch ~]#

3.[],[是shell内建命令,需与]配合结尾

[root@elasticsearch ~]# type [
[ is a shell builtin
[root@elasticsearch ~]# type ]
-bash: type: ]: not found
[root@elasticsearch ~]#

4./usr/bin/[是外部命令

[root@elasticsearch ~]# type /usr/bin/[
/usr/bin/[ is /usr/bin/[
[root@elasticsearch ~]#

2.test,/usr/bin/test,[],和/usr/bin/[用法举例

样例:

[root@elasticsearch ~]# cat test.sh 
#!/bin/bash

echo "1.test testing"
if test -z "$1"
then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo "2./usr/bin/test testing"
if /usr/bin/test -z "$1"    #与内建的"test"命令结果相同.
then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo "3.[] testing"
if [ -z "$1" ]
then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

if [ -z "$1"         #虽能运行,但是bash会报错,提示缺少关闭条件测试的右中括号. 
then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo "4./usr/bin/[ testing"
if /usr/bin/[ -z "$1" ]
then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

if /usr/bin/[ -z "$1"        #虽能运行,但是bash会报错,提示缺少关闭条件测试的右中括号.
then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo
exit 0
[root@elasticsearch ~]# sh test.sh ztj
1.test testing
First command-line argument is ztj.
2./usr/bin/test testing
First command-line argument is ztj.
3.[] testing
First command-line argument is ztj.
test.sh: line 27: [: missing `]'
First command-line argument is ztj.
4./usr/bin/[ testing
First command-line argument is ztj.
/usr/bin/[: missing ']'
First command-line argument is ztj.

[root@elasticsearch ~]#