bash shebangは何をしますか? [コピー]

bash shebangは何をしますか? [コピー]
  • bash shebangは何をしますか?
  • ./fileファイルの使用と実行の違いは何ですかsh file
    • bashはそれをどのように理解していますか?

ベストアンサー1

  • Shebangの機能は次のとおりです。

インタプリタディレクティブを使用すると、スクリプトとデータファイルをコマンドとして使用できるため、コマンドラインでインタプリタの前にスクリプトを付ける必要がなくなり、ユーザーや他のプログラムで実装の詳細を隠すことができます。

  • ./fileまたはshファイルを使用してファイルを実行することの違いは何ですか?

some/path/to/foo パスで識別される Bourne シェルスクリプトには最初の行があります。

    #!/bin/sh -x

パラメータbarとbazを使用して実行します。

    some/path/to/foo bar baz

実際、次のコマンドラインを実行するのと同様の結果が得られます。

    /bin/sh -x some/path/to/foo bar baz

注:Dennis Ritchieは1980年にインタプリタ命令のカーネルサポートを導入しました。

The system has been changed so that if a file being executed
begins with the magic characters #! , the rest of the line is understood
to be the name of an interpreter for the executed file.
Previously (and in fact still) the shell did much of this job;
it automatically executed itself on a text file with executable mode
when the text file's name was typed as a command.
Putting the facility into the system gives the following
benefits.

1) It makes shell scripts more like real executable files,
because they can be the subject of 'exec.'

2) If you do a 'ps' while such a command is running, its real
name appears instead of 'sh'.
Likewise, accounting is done on the basis of the real name.

3) Shell scripts can be set-user-ID.

注:Linuxは、解釈されたすべての実行可能ファイル(つまり、#!行で始まる実行可能ファイル)のsetuidビットを無視します。

4) It is simpler to have alternate shells available;
e.g. if you like the Berkeley csh there is no question about
which shell is to interpret a file.

5) It will allow other interpreters to fit in more smoothly.

追加情報:

おすすめ記事