/usr/bin/dash: Difference between revisions

 
(5 intermediate revisions by the same user not shown)
Line 337: Line 337:


=== Positional Parameters ===
=== Positional Parameters ===
    A positional parameter is a parameter denoted by a number (n > 0).  The shell sets these initially to
位置パラメーターは、数字(n > 0)で示されるパラメーターである。 シェルはこれらを、シェルスクリプトの名前に続くコマンドライン引数の値に初期設定する。 set 組み込み関数を使用して、これらを設定またはリセットすることもできる。
    the values of its command line arguments that follow the name of the shell script.  The set builtin
    can also be used to set or reset them.


=== Special Parameters ===
=== Special Parameters ===
    A special parameter is a parameter denoted by one of the following special characters.  The value of
特殊パラメータは、以下の特殊文字のいずれかで示されるパラメータである。 パラメータの値は、その文字の横に記載されている。
    the parameter is listed next to its character.


    *            Expands to the positional parameters, starting from one.  When the expansion occurs
;*             
                  within a double-quoted string it expands to a single field with the value of each parame‐
:位置パラメータを1から順に展開する。 展開が二重引用符で囲まれた文字列の中で行われる場合、各パラメータの値が IFS変数の最初の文字で区切られた1つのフィールドに展開され、IFSが設定されていない場合は ⟨スペース⟩で区切られる。
                  ter separated by the first character of the IFS variable, or by a ⟨space⟩ if IFS is un‐
                  set.


    @           Expands to the positional parameters, starting from one.  When the expansion occurs
;@
                  within double-quotes, each positional parameter expands as a separate argument.  If there
:位置パラメータを1から順に展開する。 展開が二重引用符で囲まれている場合、各位置パラメータは別々の引数として展開される。 位置パラメーターがない場合、@が二重引用符で囲まれていても、@の展開は0個の引数を生成する。 これが基本的に意味するのは、例えば$1が "abc "で$2が "def ghi "の場合、"$@"は2つの引数に展開されるということである:
                  are no positional parameters, the expansion of @ generates zero arguments, even when @ is
                  double-quoted.  What this basically means, for example, is if $1 is “abc” and $2 is “def
                  ghi”, then "$@" expands to the two arguments:


                        "abc" "def ghi"
::<nowiki>"abc" "def ghi"</nowiki>


    #           Expands to the number of positional parameters.
;#
:位置パラメーターの数に拡張する。


    ?           Expands to the exit status of the most recent pipeline.
;?
:直近のパイプラインの終了ステータスに展開する。


    - (Hyphen.) Expands to the current option flags (the single-letter option names concatenated into a
;- (Hyphen.)
                  string) as specified on invocation, by the set builtin command, or implicitly by the
:起動時、set 組み込みコマンド、またはシェルによって暗黙的に指定された、現在のオプションフラグ(1文字のオプション名を連結した文字列)に展開する。
                  shell.


    $           Expands to the process ID of the invoked shell.  A subshell retains the same value of $
;$
                  as its parent.
:呼び出されたシェルのプロセス ID に展開される。 サブシェルは、親と同じ $ の値を保持する。


    !           Expands to the process ID of the most recent background command executed from the current
;!
                  shell.  For a pipeline, the process ID is that of the last command in the pipeline.
:現在のシェルから実行された最新のバックグラウンド・コマンドのプロセスIDに展開される。 パイプラインの場合、プロセスIDはパイプラインの最後のコマンドのものになる。


    0 (Zero.)   Expands to the name of the shell or shell script.
;0 (Zero.)
:シェルまたはシェルスクリプトの名前に展開する。


=== Word Expansions ===
=== Word Expansions ===
    This clause describes the various expansions that are performed on words.  Not all expansions are per‐
この節では、単語に対して行われる様々な展開について説明する。 後述するように、すべての展開がすべての単語に対して行われるわけではない。
    formed on every word, as explained later.
後述するように、すべての単語に適用されるわけではない。


    Tilde expansions, parameter expansions, command substitutions, arithmetic expansions, and quote re‐
チルダ展開、パラメータ展開、コマンド置換、算術展開、引用符除去のうち、1つの単語内で行われるものは、1つのフィールドに展開される。 一つの単語から複数のフィールドを作ることができるのは、フィールドの分割かパス名の展開だけである。 このルールの唯一の例外は、上で説明したように、ダブルクォート内での特殊パラメータ@の展開である。
    movals that occur within a single word expand to a single field.  It is only field splitting or path‐
    name expansion that can create multiple fields from a single word.  The single exception to this rule
    is the expansion of the special parameter @ within double-quotes, as was described above.


    The order of word expansion is:
単語展開の順序は以下の通りである:


    1.  Tilde Expansion, Parameter Expansion, Command Substitution, Arithmetic Expansion (these all occur
#チルダ展開、パラメータ展開、コマンド置換、算術展開(これらはすべて同時に行われる)。
          at the same time).
#フィールド分割は、IFS変数がNULLでない限り、ステップ(1)で生成されたフィールドに対して行われる。
#パス名展開(set -fが有効な場合を除く)。
#引用符の除去。


    2.  Field Splitting is performed on fields generated by step (1) unless the IFS variable is null.
文字は、パラメータ展開、コマンド置換、または算術評価の導入に使用される。


    3.  Pathname Expansion (unless set -f is in effect).
チルダ展開(ユーザーのホームディレクトリを置換する)。
 
引用符で囲まれていないチルダ文字(~)で始まる単語は、チルダ展開の対象となる。 スラッシュ(/)または語尾までのすべての文字がユーザー名として扱われ、そのユーザーのホームディレクトリに置き換えられる。 ユーザー名がない場合(~/foobarのように)、チルダはHOME変数の値(現在のユーザーのホームディレクトリ)に置き換えられる。
    4.  Quote Removal.
 
    The $ character is used to introduce parameter expansion, command substitution, or arithmetic evalua‐
    tion.
 
  Tilde Expansion (substituting a user's home directory)
    A word beginning with an unquoted tilde character (~) is subjected to tilde expansion.  All the char‐
    acters up to a slash (/) or the end of the word are treated as a username and are replaced with the
    user's home directory.  If the username is missing (as in ~/foobar), the tilde is replaced with the
    value of the HOME variable (the current user's home directory).


=== Parameter Expansion ===
=== Parameter Expansion ===
    The format for parameter expansion is as follows:
パラメータ展開のフォーマットは以下の通りである:


           ${expression}
           ${expression}


    where expression consists of all characters until the matching “}”.  Any “}” escaped by a backslash or
式は、マッチする"}"までのすべての文字で構成される。 バックスラッシュでエスケープされた"}"、または引用符で囲まれた文字列内の"}"、および 埋め込み算術展開、コマンド置換、変数展開の文字は、マッチする"}"を決定する際には検査されない。
    within a quoted string, and characters in embedded arithmetic expansions, command substitutions, and
    variable expansions, are not examined in determining the matching “}”.


    The simplest form for parameter expansion is:
パラメータ展開の最も単純な形式は以下のとおりである:


           ${parameter}
           ${parameter}


    The value, if any, of parameter is substituted.
パラメータの値があれば、その値が代入される。
 
    The parameter name or symbol can be enclosed in braces, which are optional except for positional pa‐
    rameters with more than one digit or when parameter is followed by a character that could be inter‐
    preted as part of the name.  If a parameter expansion occurs inside double-quotes:


    1.  Pathname expansion is not performed on the results of the expansion.
パラメータ名または記号は中括弧で囲むことができる。中括弧は、1桁以上の位置パラメー タや、パラメータの後に名前の一部として解釈される文字が続く場合を除き、省略可能である。 パラメータ展開が二重引用符で囲まれている場合、そのパラメータは中括弧で囲むことができる:


    2.  Field splitting is not performed on the results of the expansion, with the exception of @.
#パス名展開は展開結果に対して行われない。
#フィールド分割は、@を除いて、展開結果に対して実行されない。


    In addition, a parameter expansion can be modified by using one of the following formats.
さらに、以下の書式のいずれかを使用することで、パラメータ展開を変更することができる。


    ${parameter:-word}   Use Default Values.  If parameter is unset or null, the expansion of word is
;<nowiki>${parameter:-word}</nowiki>
                          substituted; otherwise, the value of parameter is substituted.
:デフォルト値を使用する。 パラメータが未設定またはNULLの場合、wordの展開が代入され、そうでない場合、パラメータの値が代入される。


    ${parameter:=word}   Assign Default Values.  If parameter is unset or null, the expansion of word is
;<nowiki>${parameter:=word}</nowiki>
                          assigned to parameter.  In all cases, the final value of parameter is substi‐
:デフォルト値を割り当てる。 パラメータが未設定または NULL の場合、単語の展開がパラメータに代入される。 すべての場合において、パラメータの最終値が代入される。 この方法で代入できるのは変数のみで、位置パラメーターや特殊パラメーターは代入できない。
                          tuted.  Only variables, not positional parameters or special parameters, can be
                          assigned in this way.


    ${parameter:?[word]} Indicate Error if Null or Unset.  If parameter is unset or null, the expansion
;<nowiki>${parameter:?[word]}</nowiki>
                          of word (or a message indicating it is unset if word is omitted) is written to
:Nullまたは未設定の場合、エラーを表示する。 パラメータが未設定またはNULLの場合、wordの展開(wordが省略された場合は未設定であることを示すメッセージ)が標準エラーに書き込まれ、シェルは0以外の終了ステータスで終了する。 そうでなければ、パラメータの値が代入される。 対話型シェルは終了する必要はない。
                          standard error and the shell exits with a nonzero exit status.  Otherwise, the
                          value of parameter is substituted.  An interactive shell need not exit.


    ${parameter:+word}   Use Alternative Value.  If parameter is unset or null, null is substituted; oth‐
;<nowiki>${parameter:+word}</nowiki>
                          erwise, the expansion of word is substituted.
:代替値を使用する。 パラメータが未設定またはNULLの場合、NULLが代入され、そうでない場合、単語の展開が代入される。


    In the parameter expansions shown previously, use of the colon in the format results in a test for a
:先に示したパラメータ展開では、書式にコロンを使用すると、パラメータが未設定またはNULLであるかどうかをテストすることになる。
    parameter that is unset or null; omission of the colon results in a test for a parameter that is only
    unset.


    ${#parameter}         String Length.  The length in characters of the value of parameter.
;<nowiki>${#parameter}</nowiki>
:文字列の長さ。 パラメータ値の文字数。


    The following four varieties of parameter expansion provide for substring processing.  In each case,
以下の4種類のパラメータ拡張は、部分文字列処理を提供する。 いずれの場合も、パターンの評価には正規表現表記ではなく、パターンマッチング表記(シェルパターン参照)が使われる。 パラメータが*または@の場合、展開結果は不定である。 パラメータ展開文字列全体を二重引用符で囲むと、次の4種類のパターン文字は引用符で囲まれないが、中括弧内の文字は引用符で囲まれる。
    pattern matching notation (see Shell Patterns), rather than regular expression notation, is used to
    evaluate the patterns.  If parameter is * or @, the result of the expansion is unspecified.  Enclosing
    the full parameter expansion string in double-quotes does not cause the following four varieties of
    pattern characters to be quoted, whereas quoting characters within the braces has this effect.


    ${parameter%word}     Remove Smallest Suffix Pattern.  The word is expanded to produce a pattern.  The
;<nowiki>${parameter%word}</nowiki>
                          parameter expansion then results in parameter, with the smallest portion of the
:最小接尾辞パターンを取り除く。 単語を展開してパターンを生成する。 パラメータ展開の結果、パターンにマッチする接尾辞の最小部分が削除されたパラメータが得られる。
                          suffix matched by the pattern deleted.


    ${parameter%%word}   Remove Largest Suffix Pattern.  The word is expanded to produce a pattern.  The
;<nowiki>${parameter%%word}</nowiki>
                          parameter expansion then results in parameter, with the largest portion of the
:最大の接尾辞パターンを取り除く。 単語を展開してパターンを生成する。 そして、パラメータ展開の結果、パターンにマッチする接尾辞の最大部分が削除されたパラメータが生成される。
                          suffix matched by the pattern deleted.


    ${parameter#word}     Remove Smallest Prefix Pattern.  The word is expanded to produce a pattern.  The
;<nowiki>${parameter#word}</nowiki>
                          parameter expansion then results in parameter, with the smallest portion of the
:最小の接頭辞パターンを取り除く。 単語を展開してパターンを生成する。 パラメータ展開の結果、パターンにマッチする接頭辞の最小部分が削除されたパラメータが得られる。
                          prefix matched by the pattern deleted.


    ${parameter##word}   Remove Largest Prefix Pattern.  The word is expanded to produce a pattern.  The
;<nowiki>${parameter##word}</nowiki>
                          parameter expansion then results in parameter, with the largest portion of the
:最大の接頭辞パターンを取り除く。 単語を展開してパターンを生成する。 パラメータ展開の結果、パターンにマッチする接頭辞の最大部分が削除されたパラメータが得られる。
                          prefix matched by the pattern deleted.


=== Command Substitution ===
=== Command Substitution ===
    Command substitution allows the output of a command to be substituted in place of the command name it‐
コマンド置換では、コマンド名の代わりにコマンドの出力を置換することができる。 コマンド置換は、コマンドが以下のように囲まれているときに行われる:
    self.  Command substitution occurs when the command is enclosed as follows:


           $(command)
           $(command)
Line 482: Line 446:
           `command`
           `command`


    The shell expands the command substitution by executing command in a subshell environment and replac‐
シェルは、サブシェル環境でコマンドを実行し、コマンドの標準出力でコマン ド置換を置き換えることで、コマンド置換を展開し、置換の最後にある1つ以上の ⟨改行⟩のシーケンスを削除する。 (出力の末尾の前に埋め込まれた⟩は削除されないが、フィールド分割の際に、有効なIFSとquotingの値によっては、⟨space⟩に変換されることがある)
    ing the command substitution with the standard output of the command, removing sequences of one or
    more ⟨newline⟩s at the end of the substitution.  (Embedded ⟨newline⟩s before the end of the output are
    not removed; however, during field splitting, they may be translated into ⟨space⟩s, depending on the
    value of IFS and quoting that is in effect.)


=== Arithmetic Expansion ===
=== Arithmetic Expansion ===