使用 bin/sh 中的函数

问题描述 投票:0回答:0

sh
中是否可以在一个文件中定义函数并从另一个文件中调用它们并将结果分配给变量?

# pwd
/root
# ls
a.sh b.sh
# cat a.sh
the_function() {
        echo "This is the function. It says: $1"
}

# cat b.sh
#!/bin/sh

/root/a.sh

the_function 'Called directly'

r=$(the_function 'hello')

echo "Result: $r"

# ./b.sh
./b.sh: 5: ./b.sh: the_function: not found
./b.sh: 7: ./b.sh: the_function: not found
Result:
sh
© www.soinside.com 2019 - 2024. All rights reserved.