8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルスクリプトでbooleanを使う

Last updated at Posted at 2019-05-13

シェルスクリプトでもtruefalseが使えることを知ったのでメモ。

それぞれexit statusを0と1で返す性質があります。

#!/bin/bash

true
echo $? # = 0

false
echo $? # = 1

この性質を使うと下記のような制御が可能です。

#!/bin/bash

bool=true
$bool && echo "true" # = true

bool=false
# falseの時に"false"を出力
$bool || echo "false" # = false
echo $? # = 0

trueについては文字列が入ってたり未定義の場合と同じ挙動であまり使いみちが思いつかないかったですが、
falseの場合だけ処理させるケースを||を使って1行で表現できるのは便利でした。

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?