国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > test 以获得更好的体验。

# Bash 的 test 命令

test EXPRESSION

# 或

[ EXPRESSION ]

!subtitle:功能

评估表达式的布尔值;可以检查文件类型、检查字符串、比较整数大小。

!subtitle:类型

Bash 内置命令。

test[ 也是 coreutils 的一个可执行文件;为了保持 POSIX 标准的兼容性,确保在其他 shell(比如一些极简的或古老的 shell)或者某些特定环境下,test 命令依然可用。

!subtitle:说明

  • 表达式

    • ( EXPRESSION ) - EXPRESSION 是 true

    • ! EXPRESSION - EXPRESSION 是 false

    • EXPRESSION1 -a EXPRESSION2 - EXPRESSION1 和 EXPRESSION2 都是 true

    • EXPRESSION1 -o EXPRESSION2 - EXPRESSION1 或 EXPRESSION2 是 true

  • 字符串判断

    • -n STRING - STRING 长度不为 0

    • -z STRING - STRING 长度为 0

    • STRING1 = STRING2 - STRING1 等于 STRING2

    • STRING1 != STRING2 - STRING1 不等于 STRING2

  • 整数判断

    • INTEGER1 -eq INTEGER2 - INTEGER1 等于 INTEGER2

    • INTEGER1 -ne INTEGER2 - INTEGER1 不等于 INTEGER2

    • INTEGER1 -lt INTEGER2 - INTEGER1 小于 INTEGER2

    • INTEGER1 -gt INTEGER2 - INTEGER1 大于 INTEGER2

    • INTEGER1 -le INTEGER2 - INTEGER1 小于或等于 INTEGER2

    • INTEGER1 -ge INTEGER2 - INTEGER1 大于或等于 INTEGER2

  • 文件判断

    • -e FILE - 文件存在

    • -f FILE - 文件存在且是普通文件(regular file)

    • -d FILE - 文件存在且是目录(directory)

    • -b FILE - 文件存在且是块设备文件(block special)

    • -c FILE - 文件存在且是字符设备文件(character special)

    • -p FILE - 文件存在且是命名管道(named pipe)

    • -S FILE - 文件存在且是套接字文件(socket)

    • -h FILE - 文件存在且是符号链接(symbolic link),同 -L

    • -L FILE - 文件存在且是符号链接(symbolic link),同 -h

    • -r FILE - 文件存在且当前用户有读权限

    • -w FILE - 文件存在且当前用户有写权限

    • -x FILE - 文件存在且当前用户有执行权限

    • -s FILE - 文件存在且大小大于 0

    • -k FILE - 文件存在且设置了 sticky 位

    • -u FILE - 文件存在且设置了 set-user-ID

    • -g FILE - 文件存在且设置了 set-group-ID

    • -O FILE - 文件存在且所属用户 ID 是当前进程的有效用户 ID

    • -G FILE - 文件存在且所属组 ID 是当前进程的有效组 ID

    • -N FILE - 文件存在且自上次读取后已被修改

    • -t FD - 文件描述符 FD 在终端中被打开

    • FILE1 -ef FILE2 - FILE1 和 FILE2 相同(具有相同的设备和 inode 编号)

    • FILE1 -nt FILE2 - FILE1 比 FILE2 新

    • FILE1 -ot FILE2 - FILE1 比 FILE2 旧

# 示例

运行示例

if [ -d "/tmp" ]; then
    echo "/tmp 是目录"
fi

if [ -f "/tmp/code" ]; then
    echo "/tmp/code 是普通文件"
fi

if [ -c "/dev/null" ]; then
    echo "/dev/null 是字符设备"
fi

if [ 10 -gt 5 ]; then
    echo "10 大于 5"
fi

if [ 6 -lt 8 ]; then
    echo "6 小于 8"
fi

# 推荐阅读

# 手册

TEST(1)                          User Commands                         TEST(1)

NAME
       test - check file types and compare values

SYNOPSIS
       test EXPRESSION
       test
       [ EXPRESSION ]
       [ ]
       [ OPTION

DESCRIPTION
       Exit with the status determined by EXPRESSION.

       --help display this help and exit

       --version
              output version information and exit

       An omitted EXPRESSION defaults to false.  Otherwise, EXPRESSION is true
       or false and sets exit status.  It is one of:

       ( EXPRESSION )
              EXPRESSION is true

       ! EXPRESSION
              EXPRESSION is false

       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true

       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING

       -z STRING
              the length of STRING is zero

       STRING1 = STRING2
              the strings are equal

       STRING1 != STRING2
              the strings are not equal

       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2

       FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2

       FILE1 -ot FILE2
              FILE1 is older than FILE2

       -b FILE
              FILE exists and is block special

       -c FILE
              FILE exists and is character special

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

       -f FILE
              FILE exists and is a regular file

       -g FILE
              FILE exists and is set-group-ID

       -G FILE
              FILE exists and is owned by the effective group ID

       -h FILE
              FILE exists and is a symbolic link (same as -L)

       -k FILE
              FILE exists and has its sticky bit set

       -L FILE
              FILE exists and is a symbolic link (same as -h)

       -N FILE
              FILE exists and has been modified since it was last read

       -O FILE
              FILE exists and is owned by the effective user ID

       -p FILE
              FILE exists and is a named pipe

       -r FILE
              FILE exists and the user has read access

       -s FILE
              FILE exists and has a size greater than zero

       -S FILE
              FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE
              FILE exists and its set-user-ID bit is set

       -w FILE
              FILE exists and the user has write access

       -x FILE
              FILE exists and the user has execute (or search) access

       Except  for  -h  and  -L,  all  FILE-related tests dereference symbolic
       links.  Beware that parentheses need to  be  escaped  (e.g.,  by  back‐
       slashes) for shells.  INTEGER may also be -l STRING, which evaluates to
       the length of STRING.

       NOTE:  Binary  -a  and -o are inherently ambiguous.  Use 'test EXPR1 &&
       test EXPR2' or 'test EXPR1 || test EXPR2' instead.

       NOTE: [ honors the --help and --version options,  but  test  does  not.
       test treats each of those as it treats any other nonempty STRING.

       NOTE:  your shell may have its own version of test and/or [, which usu‐
       ally supersedes the version  described  here.   Please  refer  to  your
       shell's documentation for details about the options it supports.

AUTHOR
       Written by Kevin Braunsdorf and Matthew Bradburn.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       access(2)

       Full documentation <https://www.gnu.org/software/coreutils/test>
       or available locally via: info '(coreutils) test invocation'

GNU coreutils 9.4                 April 2024                           TEST(1)
本文 更新于: 2025-11-27 09:38:16 创建于: 2025-11-27 09:38:16