which は間違ったツールです。 getopts 通常はビルトインでもあります:
getopts以降 現在のシェル実行環境に影響を与えるため、通常はシェルの通常の組み込みとして提供されます。
~ for sh in dash ksh bash zsh; do "$sh" -c 'printf "%s in %s\n" "$(type getopts)" "$0"'; done
getopts is a shell builtin in dash
getopts is a shell builtin in ksh
getopts is a shell builtin in bash
getopts is a shell builtin in zsh
シェル スクリプトを使用している場合は、getopts に安全に依存できます。 .どちらかを優先する理由は他にもあるかもしれませんが、getopts 標準です .
参照:「which」を使用しないのはなぜですか?では何を使う?
私も getopts の方がいいと思います getopt以上 以下の理由により:
getopt 短所
<オール> getopts 長所
<オール> -a -b でうまく動作します -ab と同様