Linux-在gnome-terminal -x中运行bash函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux-在gnome-terminal -x中运行bash函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1312字,纯文字阅读大概需要2分钟。
内容图文
![Linux-在gnome-terminal -x中运行bash函数](/upload/InfoBanner/zyjiaocheng/948/90d92e0116ec40daad7ecba09973bdff.jpg)
我有一个bash函数,我想使用gnome终端在新窗口中执行该函数.我该怎么做?我想在blah.sh脚本中执行以下操作:
my_func() {
// Do cool stuff
}
gnome-terminal -x my_func
我现在正在做的是将my_func()放入脚本中并调用gnome-terminal -x ./my_func
解决方法:
您可以将其与export -f一起使用,就像@kojiro的上面的注释中指出的那样.
# Define function.
my_func() {
// Do cool stuff
}
# Export it, so that all child `bash` processes see it.
export -f my_func
# Invoke gnome-terminal with `bash -c` and the function name, *plus*
# another bash instance to keep the window open.
# NOTE: This is required, because `-c` invariably exits after
# running the specified command.
# CAVEAT: The bash instance that stays open will be a *child* process of the
# one that executed the function - and will thus not have access to any
# non-exported definitions from it.
gnome-terminal -x bash -c 'my_func; bash'
我从https://stackoverflow.com/a/18756584/45375借来的技术
借助一些技巧,您可以不用export -f,而可以假设运行该函数后保持打开状态的bash实例本身不需要继承my_func.
声明-f返回my_func的定义(源代码),因此只需在新的bash实例中重新定义它即可:
gnome-terminal -x bash -c "$(declare -f my_func); my_func; bash"
再一次,如果需要,您甚至可以在其中挤压export -f命令:
gnome-terminal -x bash -c "$(declare -f my_func);
export -f my_func; my_func; bash"
内容总结
以上是互联网集市为您收集整理的Linux-在gnome-terminal -x中运行bash函数全部内容,希望文章能够帮你解决Linux-在gnome-terminal -x中运行bash函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。