2
0

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 3 years have passed since last update.

bash関数の内部で`exit`した際の挙動

Posted at

Environment

  • bash

Problems

  • 関数内でexitを読んだ際、どのような挙動になるのかを知りたい。
    • 関数の実行のみが終了するのか、ファイルの実行が終了するのか
    • 終了コードはどのようになるのか

How to solve

確認したところ、以下のような挙動だった。

  • ファイルの実行が終了する
  • プロセスの終了コードは、exitで指定した終了コードとなる

Description

cat << EOF > a.sh
exit_func () {
    exit 0
}

exit_func
echo 'execute here!'
EOF

sh ./a.sh
echo $?

output

0
cat << EOF > b.sh
exit_func_254 () {
    exit 254
}

exit_func_254
echo 'execute here!'
EOF

sh ./b.sh
echo $?

output

254

exitはプロセスを終了させるものなので、よく考えれば当然の結果かもしれない。

Reference

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?