나는 배쉬를 배우고있다. 나의 현재 이해에서, 나는 서브 셸에서 그것을 사용하는 함수를 내보낼 필요가있다. 그러나 하위 셸에서 내보낼 수없는 함수를 호출 할 수 있습니다. 왜 또는 어떻게 수출되지 않은 함수가 하위 셸에서 작동하는지 알고 싶습니다. 테스트 코드는 다음입니다하위 셸에서 내보낼 수없는 함수를 호출 할 수있는 이유는 무엇입니까?
파일 이름 : level1.sh
#!/bin/bash
function level_1_func {
echo "function 1: $1, PID=${BASHPID}"
}
파일 이름 : "배쉬의 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
답변 해 주셔서 감사합니다. 나는 당신이 말하는 것처럼 하위 쉘, 포크 유형 및 exec 유형의 두 가지 유형을 구별하는 것이 더 낫다는 것에 동의합니다. 그들은 훨씬 더 명확합니다. – mora
하위 쉘이'fork' 액션이라고 말할 수 있습니다. 'fork/exec' 액션은 다른 프로그램을 실행하는 것입니다. 그것은 자식 프로세스이지만 쉘이 아닐 수도 있습니다. – cdarke