次の bash スクリプトがあります (これは、より複雑なスクリプトの簡略化されたバージョンです)。
#!/usr/bin/env bash
set -x
function execute() {
`$1` # same as $($1), gives "command not found" as do all the following:
# $1 # or ${1}
# eval "$1"
# eval $1
# This gives "No such file or directory" even though it *is* there...
#"$1"
}
function runCommand() {
PATH="${1}"
execute "chmod 777 ${PATH}"
}
execute "chmod 777 ${1}"
runCommand "$1"
#EOF
実行すると、次の出力が得られます。
+ execute 'chmod 777 build.test-case.sh'
++ chmod 777 build.test-case.sh
+ runCommand build.test-case.sh
+ PATH=build.test-case.sh
+ execute 'chmod 777 build.test-case.sh'
++ chmod 777 build.test-case.sh
./build.test-case.sh: line 5: chmod: command not found
chmod
関数が直接呼び出された場合はexecute
機能しますが、別の関数から呼び出された場合は失敗しますが、デバッグ出力はまったく同じように見えます...
誰でもこの動作を説明できますか?