2016-09-04 9 views
0

私は学習のバッシュです。 bashのマニュアルで は、私はサブシェルが自動的 彼らはexport組み込みに-fオプションで定義したように、関数定義は常にbashでエクスポートされていますか?

関数をエクスポートすることができると言う機能についての文書を発見しました。

私のbashでは、これはシェル変数とは逆の関数の定義をエクスポートします。しかし、bashマニュアルでは "exported"という単語を使用しています。関数の定義は常にbashでエクスポートされていますか?エクスポートを保証するために何かしなければなりませんか?すべての綿毛を削除

+0

@ Jonathan Leffler:質問を編集していただきありがとうございます。上の引用符を示す黄色のブロックを作る方法を教えてください。 stackoverflowのmarkdown命令でフォーマットを見つけようとしましたが、残念ながら何がどこにあるのかわかりませんでした。どうもありがとうございました。 – mora

答えて

2
Functions may be exported ... with the -f option to the export builtin. 

。それが何を言おうとしているのかははっきりしているはずです。

そして、ちょうどそれがまだない場合には

...何か(変数や関数)が輸出されているかどうか

export -f <funcname> 
+0

わかりやすい指導をいただきありがとうございます。私は1つを確認させてください。関数funcname(){...}を使って関数を定義するとき。また、-f をエクスポートしないと、関数は常に自動的にエクスポートされるのでしょうか?その行動は環境に依存するのだろうか?どうもありがとうございました。 – mora

1

は、それがサブプロセスに渡されますかどうかを決定します。シェル関数の場合、これは実際にはサブプロセスが別のシェルである場合にのみ重要です。

$ exportedfunc() { echo "This is the exported function"; } 
$ export -f exportedfunc 
$ nonexportedfunc() { echo "This is the non-exported function"; } 
$ bash # create a subshell to see which functions it inherits 
$ PS1='\$\$ ' # set a different prompt so we can tell the subshell ($$) from the parent shell ($) 
$$ exportedfunc # This'll work, because the parent shell exported the function 
This is the exported function 
$$ nonexportedfunc # This won't work because this function was not exported to subprocesses 
bash: nonexportedfunc: command not found 
$$ exit # back to the parent shell, where both functions are defined 
$ exportedfunc 
This is the exported function 
$ nonexportedfunc 
This is the non-exported function 

私はすべての機能が自動的にエクスポートすることが原因となる任意のシェルの設定を知らない:ここではイラストです。暗黙的にサブシェルを作成した場合(たとえば、カッコ内にいくつかのコマンドを入れて)、をエクスポートするかどうかは、すべてを継承します。

関連する問題