Depende do seu shell.
Este artigo exibe vários métodos .
Eu pessoalmente uso o zsh que tem uma função precmd () que é executada antes de cada prompt.
precmd () { print -Pn "\e]2;%n@%M | %~\a" } # title bar prompt
Embora as outras perguntas listem métodos bash, eles usam o cd. O Bash fornece um método inerente que encadeia apenas o prompt.
bash
bash supplies a variable PROMPT_COMMAND which contains a command to execute before the prompt. This example (inserted in ~/.bashrc) sets the title to "username@hostname: directory":
PROMPT_COMMAND='echo -ne "3]0;${USER}@${HOSTNAME}: ${PWD}\u expands to $USERNAME
\h expands to hostname up to first '.'
\w expands to directory, replacing $HOME with '~'
\[...\] embeds a sequence of non-printing characters
7"'
where 3 is the character code for ESC, and
7 for BEL. Note that the quoting is important here: variables are expanded in "...", and not expanded in '...'. So PROMPT_COMMAND is set to an unexpanded value, but the variables inside "..." are expanded when PROMPT_COMMAND is used.Thus, the following produces a prompt of "bash$ ", and an xterm title of "username@hostname: directory" ...
However, PWD produces the full directory path. If we want to use the '~' shorthand we need to embed the escape string in the prompt, which allows us to take advantage of the following prompt expansions provided by the shell:
case $TERM in
xterm*)
PS1="\[3]0;\u@\h: \w precmd () { print -Pn "\e]2;%n@%M | %~\a" } # title bar prompt
7\]bash\$ "
;;
*)
PS1="bash\$ "
;;
esac
Note the use of [...], which tells bash to ignore the non-printing control characters when calculating the width of the prompt. Otherwise line editing commands get confused while placing the cursor.
PROMPT_COMMAND='echo -ne "3]0;${USER}@${HOSTNAME}: ${PWD}\u expands to $USERNAME
\h expands to hostname up to first '.'
\w expands to directory, replacing $HOME with '~'
\[...\] embeds a sequence of non-printing characters
7"'
%bl0ck_qu0te%