「Transwiki:Bash Shell Scripting」の版間の差分

ナビゲーションに移動 検索に移動
上のスクリプトは <tt>bar</tt>と出力します。 一行のスクリプトの内部で <tt>$foo</tt> 変数を変更していますが、その変更は呼び出し側のプロセスには影響しません。(しかしそのスクリプトによって呼び出されたスクリプトには影響するでしょう。)
prints <tt>bar</tt>; the change to <tt>$foo</tt> inside the one-line script doesn't affect the process that invoked it. (However, it ''would'' affect any scripts that were called in turn ''by'' that script.)
If a given environment variable is desired for just one command, the syntax <source lang="bash" enclose="none">var=value command</source> may be used, with the syntax of a variable assignment (or multiple variable assignments) preceding a command on the same line. (Note that, despite using the syntax of a variable assignment, this is very different from a normal Bash variable assignment, in that the variable is automatically exported into the environment, and in that it only exists for the one command. If you want avoid the confusion of similar syntax doing dissimilar things, you can use the common Unix utility <tt>env</tt> for the same effect. That utility also makes it possible to ''remove'' an environment variable for one command — or even to remove ''all'' environment variables for one command.) If <tt>$var</tt> already exists, and it's desired to include its actual value in the environment for just one command, that can be written as <source lang="bash" enclose="none">var="$var" command</source>.