今日、通常はシステム上でPOSIXシェルを見つけることができるため、一般的にはPOSIX言語でスクリプトを作成できることを意味します(モジュロがコンプライアンスバグに遭遇する)。
唯一の問題は/bin/sh、POSIXシェルではない場合があることです。また、この#!行を、実行可能ファイルとして動作するスクリプトにハードコーディングする必要があります。ユーザーに問題を調査してからスクリプトを起動するように依頼することはできません/path/to/posix/shell myscript。
そのため、スクリプトでPOSIX機能を使用し、スクリプトがPOSIXシェルを自動的に検出するようにするのがコツです。その方法の1つは次のとおりです。
#!/bin/sh
# At this point, we may be running under some old shell
# we have to tread carefully.
# note how we use test rather than [ ] syntax and avoid
# depending on test with no argument producing a failure;
# i.e. "test $posix_shell".
if ! test x$posix_shell = x ; then
  # the three possible shell paths are just an example;
  # please extend as necessary.
  for shell in /usr/xpg4/bin/sh /bin/bash /usr/bin/bash ; do
    if test -x $shell ; then
       posix_shell=$shell
    fi
  done
  if test x$posix_shell = x ; then
    echo "no POSIX shell found"
    exit 1
    # or we could avoid bailing here and just fall back on /bin/sh:
    # echo "falling back on /bin/sh: cross your fingers that it works"
    # posix_shell=/bin/sh
  fi
  export posix_shell
  # plain "$@" is broken in ancient shells! 
  # I seem to recall ${@+"$@"}: not sure if that's the right trick.
  exec $posix_shell $0 ${@+"$@"}  # can we count on exec in legacy shells? 
fi
# phew, at this point in the script we have been re-executed and are
# being interpreted by some reasonably modern shell. We can use $(...)
# command substitution, and other features.
コード生成など、他のアプローチもあります。#なしでスクリプトファイルの本体を取る小さなスクリプトでスクリプトをブーストラップします。行を追加します。
最悪の事態は、1981年からBourneシェルで実行されるようにスクリプト全体を書き始めることです。これは、他のシェルをまったく持たないシステム用に記述する必要がある場合にのみ必要です。 。