LoginSignup
41
27

More than 1 year has passed since last update.

【Linux】シェルスクリプトにおける && と || の違い

Last updated at Posted at 2019-05-20

複数コマンドをAND演算で実行する &&

bashシェルスクリプト上では && はAND制御演算子である。
以下の場合、command1が成功したらcommand2を実行する。

$ command1 && command2

エラーが起こった時の挙動

; でコマンドを連結する場合、コマンド1がエラーであっても、次のコマンドが実行されるが、
&& で連結した場合はエラーが起きた時点で動作が停止する。

複数コマンドをOR演算で実行する ||

|| はOR制御演算子である。
指定したコマンド1が異常終了した時に、それに対処する形でコマンド2を実行できるので、
主に事後のエラー処理などを行なう時に使える。

以下の場合、command1が失敗した場合command2を実行します。

$ command1 || command2
41
27
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
41
27