Bash prompt execute command every time a new prompt is displayed

Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have the following prompt in bash which shows the current git branch:
PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc
which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?
bash prompt
add a comment |Â
up vote
6
down vote
favorite
I have the following prompt in bash which shows the current git branch:
PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc
which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?
bash prompt
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have the following prompt in bash which shows the current git branch:
PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc
which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?
bash prompt
I have the following prompt in bash which shows the current git branch:
PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc
which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?
bash prompt
bash prompt
edited Feb 3 '16 at 15:30
Jeff Schaller
32.6k849110
32.6k849110
asked Feb 3 '16 at 15:26
danielr1996
185210
185210
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
9
down vote
Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:
PS1+='$(git_prompt)'
2
this gives the errorbash: command substitution: line 1: syntax error near unexpected token)' bash: command substitution: line 1:git_prompt)'
â danielr1996
Feb 3 '16 at 15:36
1
That is really strange. Make a "backup" (oldPS1="$PS1") and then try:PS1='$(git_prompt) '
â Hauke Laging
Feb 3 '16 at 15:46
add a comment |Â
up vote
4
down vote
I fixed it now by using this as prompt
PS1="$greenu $r@ $redh $r: $yellowW !$r $(git_prompt) n$yellow$ $r"
before I concatenated multiple strings to one PS1 string, there seemed to be the problem.
The trick is to write a before excuting the command with $(git_prompt).
So $(git_prompt) will be evaluated when the .bashrc is evaluated and
$(git_prompt) will be evaluated everytime a new prompt is displayed
4
For anyone who finds this in the future: Note the enclosing double-quotes"around the string. Using$(git_prompt)without those doesn't work.
â dthor
Sep 21 '16 at 0:28
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
add a comment |Â
up vote
1
down vote
try single quote in your ps1
PS1+='$(git_prompt)'
i also suggest my psOne function
psOne ()
ps1tm=$1:-01;
ps1tc=(30 31 32 33 34 35 36 37 38);
PS1='$debian_chroot:+($debian_chroot)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '

This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
add a comment |Â
up vote
0
down vote
Want to see madness? This is how I construct my bash prompt:
# inspiration: http://www.stumbleupon.com/su/2LpQMi
user_host_path="$debian_chroot:+($debian_chroot) "'u@h:w'
xterm_title='[e]0;'"$user_host_path"'a]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="$xterm_title"
git_branch='$(git_current_branch " (%s)")'
line1="$line1$user_host_path$git_branch "
line2='$ '
print_time='sed -e "s/./ÃÂ/g" -re "s/.6(..)$/ bash 1/"; date "+ %T"; >&2'
color_bold='[e[0;1m]'
color_reset='[e[0m]'
PROMPT_COMMAND="_rc_=$?;$print_time;((_rc_!=0)) && PS1='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$line2'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch
I'm not a big fan of colour.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:
PS1+='$(git_prompt)'
2
this gives the errorbash: command substitution: line 1: syntax error near unexpected token)' bash: command substitution: line 1:git_prompt)'
â danielr1996
Feb 3 '16 at 15:36
1
That is really strange. Make a "backup" (oldPS1="$PS1") and then try:PS1='$(git_prompt) '
â Hauke Laging
Feb 3 '16 at 15:46
add a comment |Â
up vote
9
down vote
Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:
PS1+='$(git_prompt)'
2
this gives the errorbash: command substitution: line 1: syntax error near unexpected token)' bash: command substitution: line 1:git_prompt)'
â danielr1996
Feb 3 '16 at 15:36
1
That is really strange. Make a "backup" (oldPS1="$PS1") and then try:PS1='$(git_prompt) '
â Hauke Laging
Feb 3 '16 at 15:46
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:
PS1+='$(git_prompt)'
Your problem is that $(git_prompt) is evaluated to some constant string before it is added to $PS1. You have to add the code instead:
PS1+='$(git_prompt)'
answered Feb 3 '16 at 15:32
Hauke Laging
53.9k1282130
53.9k1282130
2
this gives the errorbash: command substitution: line 1: syntax error near unexpected token)' bash: command substitution: line 1:git_prompt)'
â danielr1996
Feb 3 '16 at 15:36
1
That is really strange. Make a "backup" (oldPS1="$PS1") and then try:PS1='$(git_prompt) '
â Hauke Laging
Feb 3 '16 at 15:46
add a comment |Â
2
this gives the errorbash: command substitution: line 1: syntax error near unexpected token)' bash: command substitution: line 1:git_prompt)'
â danielr1996
Feb 3 '16 at 15:36
1
That is really strange. Make a "backup" (oldPS1="$PS1") and then try:PS1='$(git_prompt) '
â Hauke Laging
Feb 3 '16 at 15:46
2
2
this gives the error
bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'â danielr1996
Feb 3 '16 at 15:36
this gives the error
bash: command substitution: line 1: syntax error near unexpected token )' bash: command substitution: line 1: git_prompt)'â danielr1996
Feb 3 '16 at 15:36
1
1
That is really strange. Make a "backup" (
oldPS1="$PS1") and then try: PS1='$(git_prompt) 'â Hauke Laging
Feb 3 '16 at 15:46
That is really strange. Make a "backup" (
oldPS1="$PS1") and then try: PS1='$(git_prompt) 'â Hauke Laging
Feb 3 '16 at 15:46
add a comment |Â
up vote
4
down vote
I fixed it now by using this as prompt
PS1="$greenu $r@ $redh $r: $yellowW !$r $(git_prompt) n$yellow$ $r"
before I concatenated multiple strings to one PS1 string, there seemed to be the problem.
The trick is to write a before excuting the command with $(git_prompt).
So $(git_prompt) will be evaluated when the .bashrc is evaluated and
$(git_prompt) will be evaluated everytime a new prompt is displayed
4
For anyone who finds this in the future: Note the enclosing double-quotes"around the string. Using$(git_prompt)without those doesn't work.
â dthor
Sep 21 '16 at 0:28
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
add a comment |Â
up vote
4
down vote
I fixed it now by using this as prompt
PS1="$greenu $r@ $redh $r: $yellowW !$r $(git_prompt) n$yellow$ $r"
before I concatenated multiple strings to one PS1 string, there seemed to be the problem.
The trick is to write a before excuting the command with $(git_prompt).
So $(git_prompt) will be evaluated when the .bashrc is evaluated and
$(git_prompt) will be evaluated everytime a new prompt is displayed
4
For anyone who finds this in the future: Note the enclosing double-quotes"around the string. Using$(git_prompt)without those doesn't work.
â dthor
Sep 21 '16 at 0:28
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
add a comment |Â
up vote
4
down vote
up vote
4
down vote
I fixed it now by using this as prompt
PS1="$greenu $r@ $redh $r: $yellowW !$r $(git_prompt) n$yellow$ $r"
before I concatenated multiple strings to one PS1 string, there seemed to be the problem.
The trick is to write a before excuting the command with $(git_prompt).
So $(git_prompt) will be evaluated when the .bashrc is evaluated and
$(git_prompt) will be evaluated everytime a new prompt is displayed
I fixed it now by using this as prompt
PS1="$greenu $r@ $redh $r: $yellowW !$r $(git_prompt) n$yellow$ $r"
before I concatenated multiple strings to one PS1 string, there seemed to be the problem.
The trick is to write a before excuting the command with $(git_prompt).
So $(git_prompt) will be evaluated when the .bashrc is evaluated and
$(git_prompt) will be evaluated everytime a new prompt is displayed
answered Feb 3 '16 at 20:16
danielr1996
185210
185210
4
For anyone who finds this in the future: Note the enclosing double-quotes"around the string. Using$(git_prompt)without those doesn't work.
â dthor
Sep 21 '16 at 0:28
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
add a comment |Â
4
For anyone who finds this in the future: Note the enclosing double-quotes"around the string. Using$(git_prompt)without those doesn't work.
â dthor
Sep 21 '16 at 0:28
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
4
4
For anyone who finds this in the future: Note the enclosing double-quotes
" around the string. Using $(git_prompt) without those doesn't work.â dthor
Sep 21 '16 at 0:28
For anyone who finds this in the future: Note the enclosing double-quotes
" around the string. Using $(git_prompt) without those doesn't work.â dthor
Sep 21 '16 at 0:28
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
@dthor thank you, it helped
â Herrgott
Feb 6 at 9:04
add a comment |Â
up vote
1
down vote
try single quote in your ps1
PS1+='$(git_prompt)'
i also suggest my psOne function
psOne ()
ps1tm=$1:-01;
ps1tc=(30 31 32 33 34 35 36 37 38);
PS1='$debian_chroot:+($debian_chroot)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '

This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
add a comment |Â
up vote
1
down vote
try single quote in your ps1
PS1+='$(git_prompt)'
i also suggest my psOne function
psOne ()
ps1tm=$1:-01;
ps1tc=(30 31 32 33 34 35 36 37 38);
PS1='$debian_chroot:+($debian_chroot)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '

This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
add a comment |Â
up vote
1
down vote
up vote
1
down vote
try single quote in your ps1
PS1+='$(git_prompt)'
i also suggest my psOne function
psOne ()
ps1tm=$1:-01;
ps1tc=(30 31 32 33 34 35 36 37 38);
PS1='$debian_chroot:+($debian_chroot)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '

try single quote in your ps1
PS1+='$(git_prompt)'
i also suggest my psOne function
psOne ()
ps1tm=$1:-01;
ps1tc=(30 31 32 33 34 35 36 37 38);
PS1='$debian_chroot:+($debian_chroot)[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]u[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]@[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m]h[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] :[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] w[33[$ps1tm;$ps1tc[$((RANDOM%$#ps1tc[@]))]m] $ '

edited Feb 3 '16 at 15:40
answered Feb 3 '16 at 15:34
Jonas
873515
873515
This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
add a comment |Â
This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
This looks interesting, can you explain what the ps1tm is for?
â danielr1996
Feb 3 '16 at 16:37
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
@danielr1996 it the text mode (bold unbold blink underline 0..5, in this functions it's set to 01 if no argument exists
â Jonas
Feb 3 '16 at 16:44
add a comment |Â
up vote
0
down vote
Want to see madness? This is how I construct my bash prompt:
# inspiration: http://www.stumbleupon.com/su/2LpQMi
user_host_path="$debian_chroot:+($debian_chroot) "'u@h:w'
xterm_title='[e]0;'"$user_host_path"'a]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="$xterm_title"
git_branch='$(git_current_branch " (%s)")'
line1="$line1$user_host_path$git_branch "
line2='$ '
print_time='sed -e "s/./ÃÂ/g" -re "s/.6(..)$/ bash 1/"; date "+ %T"; >&2'
color_bold='[e[0;1m]'
color_reset='[e[0m]'
PROMPT_COMMAND="_rc_=$?;$print_time;((_rc_!=0)) && PS1='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$line2'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch
I'm not a big fan of colour.
add a comment |Â
up vote
0
down vote
Want to see madness? This is how I construct my bash prompt:
# inspiration: http://www.stumbleupon.com/su/2LpQMi
user_host_path="$debian_chroot:+($debian_chroot) "'u@h:w'
xterm_title='[e]0;'"$user_host_path"'a]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="$xterm_title"
git_branch='$(git_current_branch " (%s)")'
line1="$line1$user_host_path$git_branch "
line2='$ '
print_time='sed -e "s/./ÃÂ/g" -re "s/.6(..)$/ bash 1/"; date "+ %T"; >&2'
color_bold='[e[0;1m]'
color_reset='[e[0m]'
PROMPT_COMMAND="_rc_=$?;$print_time;((_rc_!=0)) && PS1='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$line2'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch
I'm not a big fan of colour.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Want to see madness? This is how I construct my bash prompt:
# inspiration: http://www.stumbleupon.com/su/2LpQMi
user_host_path="$debian_chroot:+($debian_chroot) "'u@h:w'
xterm_title='[e]0;'"$user_host_path"'a]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="$xterm_title"
git_branch='$(git_current_branch " (%s)")'
line1="$line1$user_host_path$git_branch "
line2='$ '
print_time='sed -e "s/./ÃÂ/g" -re "s/.6(..)$/ bash 1/"; date "+ %T"; >&2'
color_bold='[e[0;1m]'
color_reset='[e[0m]'
PROMPT_COMMAND="_rc_=$?;$print_time;((_rc_!=0)) && PS1='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$line2'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch
I'm not a big fan of colour.
Want to see madness? This is how I construct my bash prompt:
# inspiration: http://www.stumbleupon.com/su/2LpQMi
user_host_path="$debian_chroot:+($debian_chroot) "'u@h:w'
xterm_title='[e]0;'"$user_host_path"'a]'
[[ $TERM == xterm* || $TERM == rxvt* ]] && line1="$xterm_title"
git_branch='$(git_current_branch " (%s)")'
line1="$line1$user_host_path$git_branch "
line2='$ '
print_time='sed -e "s/./ÃÂ/g" -re "s/.6(..)$/ bash 1/"; date "+ %T"; >&2'
color_bold='[e[0;1m]'
color_reset='[e[0m]'
PROMPT_COMMAND="_rc_=$?;$print_time;((_rc_!=0)) && PS1='$line1n$color_bold[$_rc_]$color_reset $line2' || PS1='$line1n$line2'"
unset user_host_path xterm_title color_bold color_reset line1 line2 print_time git_branch
I'm not a big fan of colour.
answered Feb 3 '16 at 16:05
glenn jackman
47.7k265104
47.7k265104
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f259618%2fbash-prompt-execute-command-every-time-a-new-prompt-is-displayed%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password