Can I get terminal title? (or otherwise restore old one)

半世苍凉 提交于 2019-12-22 06:48:38

问题


Setting terminal title is easy with echo -e "\e]0;some title\007". Works with pretty much every terminal program.

What I want is to set terminal title when some program starts - and restore old one when it finishes. Is this possible?


回答1:


On xterm, the terminal control sequences 22 and 23 work fine, as in

#!/bin/sh
/bin/echo -ne '\033[22;0t'  # Save title on stack
/bin/echo -ne "\033]0;$(date)\007"
sleep 1
/bin/echo -ne '\033[23;0t'  # Restore title from stack

It looks like this isn't supported in the Mac OS X Terminal.App though.




回答2:


There are some terminal programs that supporting it (xterm has compile time options for that, as mentioned by RWS), but most terminal programs simply lack such feature, including in particular Terminal.app.




回答3:


Yes, that is possible indeed. See a xterm reference manual (like this for example) and wander your way through it. xterm even has a build in stack for this, so you don't have to store the title manually.




回答4:


My solution was to set the window title during my script, then unset the window title when I completed. Unsetting the title reverted to the original value. Specifically, I did the following:

# Set the terminal title
printf "\e]2;%s\a" "running my script"
# Do whatever processing is required.
...

# Restore terminal title
printf "\e]2;\a"


来源:https://stackoverflow.com/questions/3232655/can-i-get-terminal-title-or-otherwise-restore-old-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!