私はbashを学んでいます。私の現在の理解では、サブシェルで使用するために関数をエクスポートする必要があります。しかし、私はサブシェルでエクスポートされていない関数を呼び出すことができます。なぜ、どのようにエクスポートされていない関数がサブシェルで動作するかを知りたい。テストコードは、以下の通りですなぜサブシェルでエクスポートされていない関数を呼び出すことができますか?
ファイル名:level1.sh
#!/bin/bash
function level_1_func {
echo "function 1: $1, PID=${BASHPID}"
}
ファイル名:「はbashのlevel0.sh
#!/bin/bash
# *** If you don't load level1.sh before this function or script,
# *** level_1_func in this function causes an error of "command not found"
function level0_func {
echo "function 0: $1, PID=${BASHPID}"
level_1_func "inside level0_func"
}
top.sh
source "./level0.sh"
# *** Error in level0_func : "level_1_func: command not found."
# *** It is because level1.sh has not been loaded.
level0_func "in the current shell"
# *** Here it loads level1.sh. After this lines,
# *** it can call level_1_func even in sub-shell.
# *** (definition of function is exported automatically?)
source "./level1.sh"
out_puts="$(level0_func "in the sub shell")"
echo "${out_puts}"
出力top.sh "は次のとおりです
function 0: in the current shell, PID=7575
./level0.sh: line 7: level_1_func: command not found
function 0: in the sub shell, PID=7577
function 1: inside level0_func, PID=7577
ありがとうございます。私は、あなたが言うように、2つのタイプのサブシェル、フォークタイプ、およびexecタイプを区別する方が良いということに同意します。彼らははるかに明確です。 – mora
私はサブシェルが 'fork'アクションだと言います。 'fork/exec'アクションは、別のプロセスを独自のプログラムで実行しているだけです。これは子プロセスですが、シェルではない可能性があります。 – cdarke