Bash - Update terminal title by running a second command
I have some answers for you :) You're right that it shouldn't matter that you're using gnome-terminal, but it does matter what command shell you're using. This is a lot easier in zsh
, but in what follows I'm going to assume you're using bash
, and that it's a fairly recent version (> 3.1).
First of all:
Which environment variable would
contain the current 'command'?
There is an environment variable which has more-or-less what you want - $BASH_COMMAND
. There's only one small hitch, which is that it will only show you the last command in a pipe. I'm not 100% sure what it will do with combinations of subshells, either :)
So I was hoping to find a way to
capture the command in bash and update
the title after every command.
I've been thinking about this, and now that I understand what you want to do, I realized the real problem is that you need to update the title before every command. This means that the $PROMPT_COMMAND
and $PS1
environment variables are out as possible solutions, since they're only executed after the command returns.
In bash
, the only way I can think of to achieve what you want is to (ab)use the DEBUG SIGNAL. So here's a solution -- stick this at the end of your .bashrc
:
trap 'printf "\033]0;%s\007" "${BASH_COMMAND//[^[:print:]]/}"' DEBUG
To get around the problem with pipes, I've been messing around with this:
function settitle () {
export PREV_COMMAND=${PREV_COMMAND}${@}
printf "\033]0;%s\007" "${BASH_COMMAND//[^[:print:]]/}"
export PREV_COMMAND=${PREV_COMMAND}' | '
}
export PROMPT_COMMAND=${PROMPT_COMMAND}';export PREV_COMMAND=""'
trap 'settitle "$BASH_COMMAND"' DEBUG
but I don't promise it's perfect!
How to change the terminal title to currently running process?
UPDATE: my previous answer (below) displays the previous command in the title bar.
Ignoring everything from my previous answer and starting from scratch:
trap 'echo -ne "\033]0;${PWD}: (${BASH_COMMAND})\007"' DEBUG
Running the following at the command prompt:
$ sleep 10
The window title bar changes to /my/current/directory: (sleep 10)
while the sleep 10
is running.
Running either of these:
$ sleep 1; sleep 2; sleep 3
$ { sleep 1; sleep2; sleep 3; }
The title bar changes as each sleep
command is invoked.
Running this:
$ ( sleep 1; sleep 2; sleep 3 )
The title bar does not change (the trap
does not apply within a subprocess call).
One last one:
$ echo $(sleep 3; echo abc)
The title bar displays (echo $sleep 3; echo abc))
.
previous answer
Adding to this answer:
store_command() {
declare -g last_command current_command
last_command=$current_command
current_command=$BASH_COMMAND
return 0
}
trap store_command DEBUG
PROMPT_COMMAND='echo -ne "\033]0;${PWD}: (${last_command})\007"'
Additional reading materials re: trap / DEBUG
:
- bash guide on traps
- SO Q&A
How to set the terminal title to show the current running command while it's running and to show it in brackets once it's finished?
You can (ab)use PS1 by putting the same escape sequence in so that every time the prompt is printed, the title gets updated.
export PS1="\033]2;[\$(history 1 | sed 's/^[ ]*[0-9]*[ ]*//g')]\007$PS1"
The final PS1 just keeps your old prompt for the actual command line.
I don't actually use this, but it should work.
How to change title of Git terminal in Windows?
You were on the right track with this link
If you modify the git-prompt.sh
script a bit (for me, this is located in c:\Program Files (x86)\Git\etc\profile.d\git-prompt.sh
), you can make the title anything you want.
Note: You will need to run VS Code, Notepad++ or similar as administrator to write back to this directory.
First, save a backup of git-prompt.sh
(like git-prompt.backup.sh
), then modify the start of git-prompt.sh
as follows:
if test -z "$GITTITLEPREFIX" # if not empty
then
GITTITLEPREFIX="Git-Bash => " # prefix that will have current pwd appended after it
fi
if test -f ~/.config/git/git-prompt.sh
then
. ~/.config/git/git-prompt.sh
else
if test -n "$GITTITLE"
then ##### Set window title directly to GITTITLE if not empty
PS1='\[\033]0;$GITTITLE\007\]'
else ##### Set window title to GITTITLE PREFIX plus the PWD
PS1='\[\033]0;$GITTITLEPREFIX${PWD//]^[:ascii:]]/?}\007\]'
fi
fi
###### Leave the rest of the file the same
PS1="$PS1"'\n'
PS1="$PS1"'\[\033[32m\]'
###### Etc.
This will first check if GITTITLEPREFIX is empty, and if not, it will set it to "Git-Bash => " similar to in the linked article. This will have the current path appended after it, so if you want "1 : $PWD", then set GITTITLEPREFIX to "1 : " like this:
GITTITLEPREFIX="1 : "
Otherwise, you can set GITTITLE to any non-empty value, and then the entire title will be set exactly to the contents of GITTITLE (no PWD appended), so if you just want "1", use this:
GITTITLE="1"
Then run the script. With my path, I did it like this:
. "/c/Program Files (x86)/Git/etc/profile.d/git-prompt.sh"
and the title should change. Of course, you can alias this or make a separate script from it in a location that is in the path so running it is much simpler, and the title could just be an argument. I'll leave that as an exercise for the reader...
Windows terminal, start two commands in tabs
By script, I hope you mean batch script, in which case try this:
wt.exe -w 1 nt PowerShell -c node "sample.js"
wt.exe -w 1 nt PowerShell -c npm test
To change the title of the tab use the --title
argument.
wt.exe --title MyCoolTab
The full list of available command line arguments can be viewed here: https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments
Ubuntu & Wine - Running multiple programs but want terminal windows not to appear
Just run wine
without using gnome-terminal
#!/bin/bash
wine software1/terminal.exe &
wine software2/terminal.exe &
wine software3/terminal.exe &
Also, to run in the background you end with &
. &&
is for testing whether a previous command completed successfully before running the next command.
Set Windows command-line terminal title in Python
This works for Python2.7 under Windows.
>>> import ctypes
>>> ctypes.windll.kernel32.SetConsoleTitleA("My New Title")
Related Topics
How to Install SQL * Plus Client in Linux
Shell Script Current Directory
Connect to Host Localhost Port 22: Connection Refused
Copy Files from Windows to Windows Subsystem for Linux (Wsl)
Limit the Memory and CPU Available for a User in Linux
How to Display Modified Date Time with 'Find' Command
Backup a Running Docker Container
PDF Compare on Linux Command Line
Installed Go Binary Not Found in Path on Alpine Linux Docker
Turning Multiple Lines into One Comma Separated Line
View a Log File in Linux Dynamically
Linux/Bash, Using Ps -O to Get Process by Specific Name
Can You Attach Amazon Ebs to Multiple Instances
Pyqt5 Error "Pycapsule_Getpointer Called with Incorrect Name"