在 helloWorld.sh
#!/bin/bash # Define a function greet greet () { echo "你好,世界!" } # Call the function greet greet
在运行脚本时,我们看到了消息
$ bash helloWorld.sh 你好,世界!
请注意,使用功能采购文件会使它们在当前bash会话中可用。
$ sourcehelloWorld.sh # or, more portably, ". helloWorld.sh" $ greet 你好,世界!
您可以export在某些外壳程序中使用某个函数,以便将其公开给子进程。
bash -c 'greet' # fails export -f greet # export function; note -f bash -c 'greet' # success