Saturday, May 18, 2024
31
rated 0 times [  31] [ 0]  / answers: 1 / hits: 7292  / 2 Years ago, tue, august 23, 2022, 11:40:54

I'm trying to have a part of my prompt set dynamically by a function, so in my .bashrc I have:



asdf ()
{
echo -n $(pwd)
}
PS1="u@h:w $(asdf)$ "


Opening a shell gives me what I expect at first:



$ bash
darthbith@server:~/test /home/darthbith/test$


However, when I change directory, the part defined by the function doesn't change:



darthbith@server:~/test /home/darthbith/test$ cd ~/test2
darthbith@server:~/test2 /home/darthbith/test$


My actual goal is to use the git-prompt.sh script to show the branch of my git repository when I'm in one with pretty colors and everything, but the problem is that it never updates the branch name when I change repositories. The trivial example above is the simplest reproduction I could come up with for my question.



The .bashrc lines that I have to integrate the git-prompt script:



source ~/.git-prompt.sh
PS1="[033[01;32m]u@h[033[00m]:[033[01;34m]w[033[00m]$(__git_ps1)$ "

More From » command-line

 Answers
5

According to Bash prompt Howto:




[21:58:33][giles@nikola:~]$ PS1="[$(date +%H%M)][u@h:w]$ "
[2159][giles@nikola:~]$ ls
bin mail
[2200][giles@nikola:~]$


It's important to notice the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment.



[#18980] Thursday, August 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cklaceowne

Total Points: 228
Total Questions: 102
Total Answers: 111

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;