Skip to content

Linux bash test statement

bash编程中if [[ ]]后面的测试选项:

1、整数测试

-le    less equal
-lt    less than
-ge    greater equal
-gt    greater than
-eq    equal
-ne    not equal

2、字符测试

==
!=
>
<
-n    测试是否为空
-s    测试是否非空

3、文件测试

-e    文件是否存在
-f    是否为普通文件
-d    指定路径是否为目录
-r    文件对当前用户是否可读
-w    文件对当前用户是否可写
-x    文件对当前用户是否可执行

例如,判断文件是否存在:

if [[ -e /etc/passwd ]]; then
  echo "File exist"
else
  echo "File not exist"
fi

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try my iOS App