LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: set Konsole window title to current command (https://www.linuxquestions.org/questions/programming-9/bash-set-konsole-window-title-to-current-command-435881/)

bforbes 04-17-2006 03:00 AM

Bash: set Konsole window title to current command
 
I've been playing around with bash variables like PROMPT_COMMAND and PS1, trying to get the command being executed to be displayed as the Konsole window title. The problem is, both of the above parameters are printed after the command is finished. If I'm running a program that doesn't finish straight away, I'd like the window title to display some info about it. Can this be done?

This is an example of what I've already tried:
Code:

export PROMPT_COMMAND='_last=`fc -l -1 | cut -f 2`;echo -en "\e]0;${_last}\a"'
It doesn't update the window title until after a subsequent command is executed.

I'm running Suse 9.3, Bash 3.00.16(1)-release-(x86_64-suse-linux).

jlliagre 04-18-2006 11:54 AM

This seems to me the expected behaviour.
Your prompt command is retrieving the last command stored in history, but at the time it is retrieved, the command just executed is not yet there.

What you really want is a way for the shell to know the next command to come, which obviously it cannot guess.

You could achieve that by using a wrapper around the real commands.

bforbes 04-18-2006 09:32 PM

How would I create this wrapper? I assume it would just be a script that is called before every command is executed, but I don't know how to do that.

jlliagre 04-18-2006 10:08 PM

You should call the wrapper instead of the command, passing it the real command to execute.
One way to achieve that would be to have a custom directory in the PATH, where all commands requiring a feedback present as a link to a shell script that set the window title, clear the PATH prefix and call the real command.

The script itself would be something like this, assuming it is in /wrapper/<commands>
Code:

#!/bin/ksh
PATH=$(print $PATH | sed 's/^\/wrapper://')
print -en "\033]0;$0\a"
exec $0 "$@"


bforbes 04-18-2006 11:41 PM

Thanks, that solution will probably do the trick for what I need, but can you think of a way to make it work for any arbitrary commands?

jlliagre 04-19-2006 11:44 AM

You'll need to patch the shell for that, and the tricky part would be to make sure only the topmost command is catch, and not all the internal sub commands.


All times are GMT -5. The time now is 03:03 PM.