Linux 在gnome终端-x中运行bash函数
我有一个bash函数,我想使用gnome终端在一个新窗口中执行该函数。我该怎么做?我想在我的blah.sh脚本中执行类似的操作:Linux 在gnome终端-x中运行bash函数,linux,bash,shell,gnome-terminal,Linux,Bash,Shell,Gnome Terminal,我有一个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 func
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'
我借用了他的技巧
通过一些技巧,您可以不使用export-f
,假设在运行函数后保持打开状态的bash实例本身不需要继承my_func
declare-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"
您是否尝试过使用export-f
导出函数?我只是尝试了export-f my_func
,然后运行gnome terminal-x my_func
,但没有结果。真可惜,太棒了!进程终止后,我不需要窗口保持打开状态,因此我跳过了my_func;bash
。谢谢你的好东西@用户985030:我的荣幸。我很高兴它对你有用。