/usr/bin/bash: Difference between revisions

Line 986: Line 986:


== ALIASES ==
== ALIASES ==
      Aliases allow a string to be substituted for a word when it is used as the first word of a simple
エイリアスを使用すると、単純なコマンドの最初の単語として文字列を使用するときに、その文字列を単語の代わりにすることができる。 シェルはエイリアスのリストを保持し、aliasとunalias組み込みコマンドで設定したり解除したりできる(後述のシェル組み込みコマンドを参照)。 各単純コマンドの最初の単語は、引用符で囲まれていない場合、エイリアスを持っているかどうかチェックされる。 もしあれば、その単語はエイリアスのテキストに置き換えられる。 文字 /, $, `, = や、上に挙げたシェルのメタ文字や引用符は、エイリアス名には使えない。 置換テキストには、シェルのメタキャラクタを含む有効なシェル入力を含めることができる。 置換テキストの最初の単語はエイリアスかどうかテストされるが、展開されるエイリアスと同じ単語は2度展開されない。 つまり、例えばlsをls -Fにエイリアスしても、bashは置換テキストを再帰的に展開しようとしない。 エイリアス値の最後の文字が空白の場合、エイリアスに続く次のコマンド語もエイリアス展開がチェックされる。
      command.  The shell maintains a list of aliases that may be set and unset with the alias and una‐
      lias builtin commands (see SHELL BUILTIN COMMANDS below).  The first word of each simple command,
      if unquoted, is checked to see if it has an alias.  If so, that word is replaced by the  text  of
      the  alias.  The characters /, $, `, and = and any of the shell metacharacters or quoting charac‐
      ters listed above may not appear in an alias name.  The replacement text may  contain  any  valid
      shell  input, including  shell metacharacters.  The first word of the replacement text is tested
      for aliases, but a word that is identical to an alias being expanded is  not  expanded  a  second
      time.  This  means that one may alias ls to ls -F, for instance, and bash does not try to recur‐
      sively expand the replacement text.  If the last character of the alias value is  a  blank,  then
      the next command word following the alias is also checked for alias expansion.


      Aliases are created and listed with the alias command, and removed with the unalias command.
エイリアスは、aliasコマンドで作成、リストされ、unaliasコマンドで削除される。


      There  is  no  mechanism for using arguments in the replacement text.  If arguments are needed, a
置換テキストに引数を使う仕組みはない。 引数が必要な場合は、シェル関数を使うべきである(下記のFUNCTIONSを参照)。
      shell function should be used (see FUNCTIONS below).


      Aliases are not expanded when the shell is not interactive, unless the expand_aliases  shell  op‐
shoptを使用してexpand_aliasesシェルオプションが設定されていない限り、シェルが対話的でないときにはエイリアスは展開されない(shoptについては、後述の[[#SHELL BUILTIN COMMANDS|SHELL BUILTIN COMMANDS ]]の説明を参照のこと)。
      tion is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).


      The rules concerning the definition and use of aliases are somewhat confusing.  Bash always reads
エイリアスの定義と使用に関するルールは少々わかりにくい。 Bashは常に、少なくとも1行の入力と、複合コマンドを構成するすべての行を読み込んでから、その行のコマンドや複合コマンドを実行する。 エイリアスは、コマンドが実行されるときではなく、読み込まれるときに展開される。 したがって、他のコマンドと同じ行に現れたエイリアス定義は、次の入力行が読み込まれるまで有効にならない。 その行のエイリアス定義に続くコマンドは、新しいエイリアスの影響を受けない。 この動作は、関数が実行される際にも問題となる。 関数定義はそれ自体がコマンドであるため、エイリアスは関数が実行されるときではなく、関数定義が読み込まれるときに展開される。 結果として、関数内で定義されたエイリアスは、その関数が実行されるまで使用できない。 安全のため、エイリアス定義は常に別の行に記述し、複合コマンドではエイリアスを使用しないこと。
      at least one complete line of input, and all lines that make up a compound command,  before  exe‐
      cuting  any  of  the  commands on that line or the compound command.  Aliases are expanded when a
      command is read, not when it is executed.  Therefore, an alias definition appearing on  the  same
      line  as another command does not take effect until the next line of input is read.  The commands
      following the alias definition on that line are not affected by the new alias.  This behavior  is
      also  an  issue  when functions are executed.  Aliases are expanded when a function definition is
      read, not when the function is executed, because a function definition is itself a command.  As a
      consequence,  aliases  defined  in a function are not available until after that function is exe‐
      cuted.  To be safe, always put alias definitions on a separate line, and do not use alias in com‐
      pound commands.


      For almost every purpose, aliases are superseded by shell functions.
ほとんどすべての目的において、エイリアスはシェル関数に取って代わられる。


== FUNCTIONS ==
== FUNCTIONS ==