条件分岐 ~ test で条件判定(基本)

test ~ 論理演算子

条件 説明
EXPRESSION EXPRESSION が true
! EXPRESSION EXPRESSION が false
EXPRESSION1 -a EXPRESSION2(非推奨) EXPRESSION1 と EXPRESSION2 がどちらも true
EXPRESSION1 -o EXPRESSION2(非推奨) EXPRESSION1 か EXPRESSION2 のどちらか true
code
echo '評価結果が true の場合は終了ステータスが 0'

test ok
echo "test ok   : \$?=$?"

test ''
echo "test ''   : \$?=$?"

test ! ok
echo "test ! ok : \$?=$?"

test ! ''
echo "test ! '' : \$?=$?"
stdout
評価結果が true の場合は終了ステータスが 0
test ok   : $?=0
test ''   : $?=1
test ! ok : $?=1
test ! '' : $?=0
code
test ok -a ok
echo "ok -a ok : \$?=$?"

test ok -a ''
echo "ok -a '' : \$?=$?"

test '' -a ok
echo "'' -a ok : \$?=$?"

test '' -a ''
echo "'' -a '' : \$?=$?"
stdout
ok -a ok : $?=0
ok -a '' : $?=1
'' -a ok : $?=1
'' -a '' : $?=1
code
test ok -o ok
echo "ok -o ok : \$?=$?"

test ok -o ''
echo "ok -o '' : \$?=$?"

test '' -o ok
echo "'' -o ok : \$?=$?"

test '' -o ''
echo "'' -o '' : \$?=$?"
stdout
ok -o ok : $?=0
ok -o '' : $?=0
'' -o ok : $?=0
'' -o '' : $?=1

-a と -o 演算子は非推奨と test コマンドの man ドキュメントに記載があります。

NOTE:Binary -a and -o are inherently ambiguous.Use 'test EXPR1 && test EXPR2' or 'test EXPR1 || test EXPR2' instead.
code
# -a 演算子は非推奨のため && の使用が推奨

test ok && test ok
echo "test ok && test ok : $?"

test ok && test ''
echo "test ok && test '' : $?"

test '' && test ok
echo "test '' && test ok : $?"

test '' && test ''
echo "test '' && test '' : $?"
stdout
test ok && test ok : 0
test ok && test '' : 1
test '' && test ok : 1
test '' && test '' : 1
code
# -o 演算子は非推奨のため || の使用が推奨

test ok || test ok
echo "test ok || test ok : $?"

test ok || test ''
echo "test ok || test '' : $?"

test '' || test ok
echo "test '' || test ok : $?"

test '' || test ''
echo "test '' || test '' : $?"
stdout
test ok || test ok : 0
test ok || test '' : 0
test '' || test ok : 0
test '' || test '' : 1

test ~ 文字列の演算子

条件 説明
-n STRING STRING の文字数が 0 ではない
-z STRING STRING の文字数が 0 である
STRING1 = STRING2 STRING1 と STRING2 が等しい
STRING1 == STRING2 STRING1 と STRING2 が等しい
STRING1 != STRING2 STRING1 と STRING2 が等しくない
STRING1 < STRING2 STRING1 が STRING2 より辞書順で前にある
STRING1 > STRING2 STRING1 が STRING2 より辞書順で後にある
code
test -n ''
echo "test -n '' : \$?=$?"

test -n ok
echo "test -n ok : \$?=$?"

test -z ''
echo "test -z '' : \$?=$?"

test -z ok
echo "test -z ok : \$?=$?"
stdout
test -n '' : $?=1
test -n ok : $?=0
test -z '' : $?=0
test -z ok : $?=1
code
test '' = ''
echo "test '' = ''   : \$?=$?"

test '' = ok
echo "test '' = ok   : \$?=$?"

test '' = 'ok'
echo "test '' = 'ok' : \$?=$?"

test ok = ok
echo "test ok = ok   : \$?=$?"
stdout
test '' = ''   : $?=0
test '' = ok   : $?=1
test '' = 'ok' : $?=1
test ok = ok   : $?=0
code
test '' == ''
echo "test '' == ''   : \$?=$?"

test '' == ok
echo "test '' == ok   : \$?=$?"

test '' == 'ok'
echo "test '' == 'ok' : \$?=$?"

test ok == ok
echo "test ok == ok   : \$?=$?"
stdout
test '' == ''   : $?=0
test '' == ok   : $?=1
test '' == 'ok' : $?=1
test ok == ok   : $?=0
code
test '' != ''
echo "test '' != ''   : \$?=$?"

test '' != ok
echo "test '' != ok   : \$?=$?"

test ok != ok
echo "test ok != ok   : \$?=$?"

test ok != 'ok'
echo "test ok != 'ok' : \$?=$?"
stdout
test '' != ''   : $?=1
test '' != ok   : $?=0
test ok != ok   : $?=1
test ok != 'ok' : $?=1
code
test A \< B
echo "test A <\ B   : \$?=$?"

test B \< A
echo "test B \< A   : \$?=$?"

test A \< A
echo "test A \< A   : \$?=$?"

test \' \< ''
echo "test \' \< '' : \$?=$?"

test \' \< A
echo "test \' \< A  : \$?=$?"
stdout
test A <\ B   : $?=0
test B \< A   : $?=1
test A \< A   : $?=1
test \' \< '' : $?=1
test \' \< A  : $?=0
code
test A \> B
echo "test A \> B   : \$?=$?"

test B \> A
echo "test B \> A   : \$?=$?"

test A \> A
echo "test A \> A   : \$?=$?"

test \' \> ''
echo "test \' \> '' : \$?=$?"

test \' \> A
echo "test \' \> A  : \$?=$?"
stdout
test A \> B   : $?=1
test B \> A   : $?=0
test A \> A   : $?=1
test \' \> '' : $?=0
test \' \> A  : $?=1

test ~ 数値の演算子

条件 不等号説明
INTEGER1 -eq INTEGER2== INTEGER1 と INTEGER2 が等しい
INTEGER1 -ge INTEGER2>= INTEGER1 は INTEGER2 以上
INTEGER1 -gt INTEGER2> INTEGER1 は INTEGER2 より大きい
INTEGER1 -le INTEGER2<= INTEGER1 は INTEGER2 以下
INTEGER1 -lt INTEGER2< INTEGER1 は INTEGER2 より小さい
INTEGER1 -ne INTEGER2!= INTEGER1 と INTEGER2 は等しくない
code
test 1 -eq 1
echo "test 1 -eq 1 : \$?=$?"

test 1 -eq 0
echo "test 1 -eq 0 : \$?=$?"
stdout
test 1 -eq 1 : $?=0
test 1 -eq 0 : $?=1
code
test 1 -ge -1
echo "1 -ge -1 : \$?=$?"

test 1 -ge 0
echo "1 -ge  0 : \$?=$?"

test 1 -ge 1
echo "1 -ge  1 : \$?=$?"

test 1 -ge 2
echo "1 -ge  2 : \$?=$?"
stdout
1 -ge -1 : $?=0
1 -ge  0 : $?=0
1 -ge  1 : $?=0
1 -ge  2 : $?=1
code
test 1 -gt -1
echo "1 -gt -1 : \$?=$?"

test 1 -gt 0
echo "1 -gt  0 : \$?=$?"

test 1 -gt 1
echo "1 -gt  1 : \$?=$?"

test 1 -gt 2
echo "1 -gt  2 : \$?=$?"
stdout
1 -gt -1 : $?=0
1 -gt  0 : $?=0
1 -gt  1 : $?=1
1 -gt  2 : $?=1
code
test 1 -le -1
echo "1 -le -1 : \$?=$?"

test 1 -le 0
echo "1 -le  0 : \$?=$?"

test 1 -le 1
echo "1 -le  1 : \$?=$?"

test 1 -le 2
echo "1 -le  2 : \$?=$?"
stdout
1 -le -1 : $?=1
1 -le  0 : $?=1
1 -le  1 : $?=0
1 -le  2 : $?=0
code
test 1 -lt -1
echo "1 -lt -1 : \$?=$?"

test 1 -lt 0
echo "1 -lt  0 : \$?=$?"

test 1 -lt 1
echo "1 -lt  1 : \$?=$?"

test 1 -lt 2
echo "1 -lt  2 : \$?=$?"
stdout
1 -lt -1 : $?=1
1 -lt  0 : $?=1
1 -lt  1 : $?=1
1 -lt  2 : $?=0
code
test 1 -ne 1
echo "test 1 -ne 1 : \$?=$?"

test 1 -ne 0
echo "test 1 -ne 0 : \$?=$?"
stdout
test 1 -ne 1 : $?=1
test 1 -ne 0 : $?=0

test ~ シェルオプション・シェル変数のチェック

条件 説明
-o OPTNAMEOPTNAME のシェルオプションが有効になっている
-v VARNAMEVARNAME のシェル変数が定義されている
code
set -o noglob +o pipefail
shopt -o | grep -E '^(noglob|pipefail)'
echo '------------------------------'

test -o noglob
echo "test -o noglob   : \$?=$?"
test -o pipefail
echo "test -o pipefail : \$?=$?"
stdout
noglob         	on
pipefail       	off
------------------------------
test -o noglob   : $?=0
test -o pipefail : $?=1
code
var1=

# シェル変数 var1 は定義されているが var2 は定義されていない
test -v var1
echo "test -v var1 : \$?=$?"
test -v var2
echo "test -v var2 : \$?=$?"
stdout
test -v var1 : $?=0
test -v var2 : $?=1