Assuming you're using Bash as your shell you can set this variable.
PROMPT_COMMAND='echo -ne "\033]0;Terminal | mplayer\007"'
Examples



Including the previous command in the title?
If you want a more elaborate title bar then you could use this method to dynamically set the currently running command in the title bar.
$ trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG
Example


What you're looking for
To get what you want you'll need to do some work to parse out just the name of the executable, for example. So you'll have to make some decisions as to what part of the command you want. You could use .. | awk '{print $1}'
to get the name of just the command.
Making it permanent
If you want to make this the default behavior via your /etc/bashrc
or $HOME/.bashrc
files, you'll likely need to follow @simon's suggestions in this SO Q&A titled: Bash - Update terminal title by running a second command.
Simon's answer is as follows:
function settitle () {
export PREV_COMMAND=${PREV_COMMAND}${@}
echo -ne "\033]0;${PREV_COMMAND}\007"
export PREV_COMMAND=${PREV_COMMAND}' | '
}
export PROMPT_COMMAND=${PROMPT_COMMAND}';export PREV_COMMAND=""'
trap 'settitle "$BASH_COMMAND"' DEBUG
Redirection of output
As referenced in this SO Q&A titled: Why is my DEBUG trap executed (w/ content redirected) in { echo foo; echo bar; } >file?
there's an answer there by @Charles Duffy which recommends redirecting output to STDERR
or even better /dev/tty
. His answer has good guidances on both these methods.
References
trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG
to my.bashrc
to have this setting permanently. But now for every new urxvt terminal opened, I am getting this output:0;%s@%s:%s" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
. How can I suppress that? – orschiro Dec 06 '13 at 14:21$PS1
would be the way to go. – slm Dec 06 '13 at 14:36trap
command to a variable to add it toPS1
. But how do I include theDEBUG
in the variable assignment? For now my.bashrc
looks as follows: http://pastebin.com/jqbE7W2Y – orschiro Dec 06 '13 at 15:07DEBUG
trap interferes with your$PROMPT_COMMAND
, which is also trying to set the title. I worked around it with a bit of awk:
– sqweek Nov 17 '15 at 05:56trap 'echo $BASH_COMMAND |awk '\''!/\007/ {printf "\033]0;%s\007", $0}'\' DEBUG
\033
is 0x1B, the escape indicator, but the]
seems to indicate that it's console-specific, and I can't find the specific thing for Bash anywhere. – May 20 '16 at 21:30\033]0;
and\007
? What doestrap
do which makes it helpful ? MyPROMPT_COMMAND
is "history -a" and I'd like to keep that part, how does that interact with your answer ? I find this answer easy to use, but hard to understand. (Looks tailored to help vampires :D) – Nikana Reklawyks Jul 12 '17 at 17:31export
s here? My expectation reading this code is that the variables are all used inside the same process, so just directly assigning them (without any explicitdeclare
orlocal
to prevent them from being shell-global) should suffice. (Happy to propose my own edit, if that works -- I believe my rep on this site is low enough that it would go in the review queue for your approval). – Charles Duffy Jan 23 '18 at 21:26DEBUG
trap's output goes to/dev/tty
to avoid causing bugs such as Why is my DEBUG trap executed (w/ content redirected) in { echo foo; echo bar; } >file? – Charles Duffy Mar 26 '19 at 11:52