Linuxシェルでハイエンドコード(> = 256)が期待どおりに機能しないのはなぜですか?

Linuxシェルでハイエンドコード(> = 256)が期待どおりに機能しないのはなぜですか?

奇妙な動作が見つかりました(zshとbashを使用して私のシステムで再現可能)。

$ # here everything is still normal
$ bash -c 'exit 1';echo $?
1
$ bash -c 'exit 255';echo $?
255
$ zsh -c 'exit 255';echo $?
255
$ # now it get's crazy
$ bash -c 'exit 256';echo $?
0
$ zsh -c 'exit 256';echo $?
0
$ # (leaving away zsh for now, it is always reproducible with both)
$ bash -c 'exit 257';echo $?
1
$ bash -c 'exit 267';echo $?
11

したがって、256以降は再び1から計算され始めます。しかし、なぜ?

Bash のマニュアルページには最大数は表示されません。

   exit [n]
          Cause  the  shell  to exit with a status of n.  If n is omitted,
          the exit status is that of the last command executed.  A trap on
          EXIT is executed before the shell terminates.

これは非常に混乱した行動です。プログラムがそれに依存している場合、大きな問題が発生する可能性があります。

それでは、なぜこれが起こるのですか?なぜ録音されていないのですか?

x64、Fedora 26

ベストアンサー1

マニュアルに記載されています。出口システム機能:

ステータス値は0、EXIT_SUCCESS、EXIT_FAILURE、[CX] [Option Start]、またはその他の値ですが、待機中の親プロセスには最下位8ビット(ステータスおよび0377など)のみを使用できます。 [オプション終了]

Linuxはこの規格に非常に厳密に準拠しているように見え、最後の8ビットのみを通過させることができます。

おすすめ記事