在shell中导出函数 [英] Exporting a function in shell

查看:148
本文介绍了在shell中导出函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我如何在父shell(bash,sh或ksh)中导出函数,以便该函数可以从父进程启动的所有子进程使用?

解决方案

export -f 功能特定于Bash:



parent

 #!/ bin / bash 
plus1(){echo $(($ 1 + 1)) ; }
echo $(plus1 8)
export -f plus1
./child 14 21

孩子

 #!/ bin / bash 
echo $(plus1 $(($ 1 * $ 2)))


Please tell me how to export a function in parent shell (bash, sh or ksh) so that the function will be available to all the child process launced from the parent process?

解决方案

The export -f feature is specific to Bash:

parent

#!/bin/bash
plus1 () { echo $(($1 + 1)); }
echo $(plus1 8)
export -f plus1
./child 14 21

child

#!/bin/bash
echo $(plus1 $(($1 * $2)) )

这篇关于在shell中导出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆