Set last command exit code to bash prompt
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:
user:~/Documents [14:22:07] 1 $
My PS1 is as follows:
ALERT_COLOR="$(tput setaf 1)"
# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi
parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
Everything works apart from the exit code. It never displays the exit code, and if I just do $?
in my PS1, then it always shows as 0
bash prompt bashrc
New contributor
add a comment |Â
up vote
2
down vote
favorite
I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:
user:~/Documents [14:22:07] 1 $
My PS1 is as follows:
ALERT_COLOR="$(tput setaf 1)"
# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi
parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
Everything works apart from the exit code. It never displays the exit code, and if I just do $?
in my PS1, then it always shows as 0
bash prompt bashrc
New contributor
Welcome to U&L StackExchange! When you say "if I just do$?
in my PS1...", how did you do that?
â JigglyNaga
38 mins ago
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
36 mins ago
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
13 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:
user:~/Documents [14:22:07] 1 $
My PS1 is as follows:
ALERT_COLOR="$(tput setaf 1)"
# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi
parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
Everything works apart from the exit code. It never displays the exit code, and if I just do $?
in my PS1, then it always shows as 0
bash prompt bashrc
New contributor
I am running Ubuntu 18.04, and I would like to set my bash prompt so it reads like below:
user:~/Documents [14:22:07] 1 $
My PS1 is as follows:
ALERT_COLOR="$(tput setaf 1)"
# Display unsuccessful exit codes
function exit_status
last_status=$?
if [[ $last_status != 0 ]]; then
echo "$ALERT_COLOR[$last_status]"
fi
parse_git_branch() sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
if [ "$color_prompt" = yes ]; then
PS1='$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m] [33[36m][t] [$ALERT_COLOR]$exit_status [33[0;37m]$ '
else
PS1='$debian_chroot:+($debian_chroot)u:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt
Everything works apart from the exit code. It never displays the exit code, and if I just do $?
in my PS1, then it always shows as 0
bash prompt bashrc
bash prompt bashrc
New contributor
New contributor
edited 38 mins ago
Goro
2,91741949
2,91741949
New contributor
asked 45 mins ago
Luke
112
112
New contributor
New contributor
Welcome to U&L StackExchange! When you say "if I just do$?
in my PS1...", how did you do that?
â JigglyNaga
38 mins ago
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
36 mins ago
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
13 mins ago
add a comment |Â
Welcome to U&L StackExchange! When you say "if I just do$?
in my PS1...", how did you do that?
â JigglyNaga
38 mins ago
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
36 mins ago
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
13 mins ago
Welcome to U&L StackExchange! When you say "if I just do
$?
in my PS1...", how did you do that?â JigglyNaga
38 mins ago
Welcome to U&L StackExchange! When you say "if I just do
$?
in my PS1...", how did you do that?â JigglyNaga
38 mins ago
2
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
36 mins ago
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
36 mins ago
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
13 mins ago
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
13 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
The issue is here:
PS1='...[$ALERT_COLOR]$exit_status ...$ '
^^
That is a parameter expansion, it doesn't call the function you've set up.
You need to call the function within a command substitution, e.g. $(exit_status)
, or from PROMPT_COMMAND
. If you do, take care with the [ .. ]
escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).
And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:
In addition, the following table describes the special characters which can appear in the prompt variables
PS1
toPS4
: [...]
After the string is decoded, it is expanded via parameter expansion, command substitution, [...]
Something like this should work:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
set_exit_color()
if [ "$?" != 0 ]; then
exit_color=$red_color
else
exit_color=$normal_color
fi
PROMPT_COMMAND=set_exit_color
PS1='[$exit_color][$?][$normal_color] w$ '
I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND
doesn't modify the value of $?
that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
exit_color()
exit_code=$?
if [ "$exit_code" != 0 ]; then
echo "$red_color"
else
echo "$normal_color"
fi
return "$exit_code"
PS1='[$(exit_color)][$?][$normal_color] w$ '
I would use the version with PROMPT_COMMAND
, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
The issue is here:
PS1='...[$ALERT_COLOR]$exit_status ...$ '
^^
That is a parameter expansion, it doesn't call the function you've set up.
You need to call the function within a command substitution, e.g. $(exit_status)
, or from PROMPT_COMMAND
. If you do, take care with the [ .. ]
escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).
And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:
In addition, the following table describes the special characters which can appear in the prompt variables
PS1
toPS4
: [...]
After the string is decoded, it is expanded via parameter expansion, command substitution, [...]
Something like this should work:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
set_exit_color()
if [ "$?" != 0 ]; then
exit_color=$red_color
else
exit_color=$normal_color
fi
PROMPT_COMMAND=set_exit_color
PS1='[$exit_color][$?][$normal_color] w$ '
I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND
doesn't modify the value of $?
that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
exit_color()
exit_code=$?
if [ "$exit_code" != 0 ]; then
echo "$red_color"
else
echo "$normal_color"
fi
return "$exit_code"
PS1='[$(exit_color)][$?][$normal_color] w$ '
I would use the version with PROMPT_COMMAND
, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.
add a comment |Â
up vote
5
down vote
The issue is here:
PS1='...[$ALERT_COLOR]$exit_status ...$ '
^^
That is a parameter expansion, it doesn't call the function you've set up.
You need to call the function within a command substitution, e.g. $(exit_status)
, or from PROMPT_COMMAND
. If you do, take care with the [ .. ]
escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).
And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:
In addition, the following table describes the special characters which can appear in the prompt variables
PS1
toPS4
: [...]
After the string is decoded, it is expanded via parameter expansion, command substitution, [...]
Something like this should work:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
set_exit_color()
if [ "$?" != 0 ]; then
exit_color=$red_color
else
exit_color=$normal_color
fi
PROMPT_COMMAND=set_exit_color
PS1='[$exit_color][$?][$normal_color] w$ '
I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND
doesn't modify the value of $?
that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
exit_color()
exit_code=$?
if [ "$exit_code" != 0 ]; then
echo "$red_color"
else
echo "$normal_color"
fi
return "$exit_code"
PS1='[$(exit_color)][$?][$normal_color] w$ '
I would use the version with PROMPT_COMMAND
, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.
add a comment |Â
up vote
5
down vote
up vote
5
down vote
The issue is here:
PS1='...[$ALERT_COLOR]$exit_status ...$ '
^^
That is a parameter expansion, it doesn't call the function you've set up.
You need to call the function within a command substitution, e.g. $(exit_status)
, or from PROMPT_COMMAND
. If you do, take care with the [ .. ]
escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).
And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:
In addition, the following table describes the special characters which can appear in the prompt variables
PS1
toPS4
: [...]
After the string is decoded, it is expanded via parameter expansion, command substitution, [...]
Something like this should work:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
set_exit_color()
if [ "$?" != 0 ]; then
exit_color=$red_color
else
exit_color=$normal_color
fi
PROMPT_COMMAND=set_exit_color
PS1='[$exit_color][$?][$normal_color] w$ '
I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND
doesn't modify the value of $?
that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
exit_color()
exit_code=$?
if [ "$exit_code" != 0 ]; then
echo "$red_color"
else
echo "$normal_color"
fi
return "$exit_code"
PS1='[$(exit_color)][$?][$normal_color] w$ '
I would use the version with PROMPT_COMMAND
, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.
The issue is here:
PS1='...[$ALERT_COLOR]$exit_status ...$ '
^^
That is a parameter expansion, it doesn't call the function you've set up.
You need to call the function within a command substitution, e.g. $(exit_status)
, or from PROMPT_COMMAND
. If you do, take care with the [ .. ]
escapes: as far as I've seen, Bash interprets them before expansions in the prompt, so you have to hardcode them in the prompt string (they can't be parts of variables or other things expanded in the prompt).
And if not expanding the prompt escapes from variables seems backwards to you, I can't blame you. But that's the way it's documented:
In addition, the following table describes the special characters which can appear in the prompt variables
PS1
toPS4
: [...]
After the string is decoded, it is expanded via parameter expansion, command substitution, [...]
Something like this should work:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
set_exit_color()
if [ "$?" != 0 ]; then
exit_color=$red_color
else
exit_color=$normal_color
fi
PROMPT_COMMAND=set_exit_color
PS1='[$exit_color][$?][$normal_color] w$ '
I would have thought it'd need a temporary variable to hold the exit status, but apparently PROMPT_COMMAND
doesn't modify the value of $?
that's expanded in the prompt. If call the function with a command substitution from inside the prompt string, then you need a workaround, as the exit code of the command substitution takes effect. Something like this:
normal_color=$'33[00m'
red_color=$'33[41m'
exit_color=$normal_color
exit_color()
exit_code=$?
if [ "$exit_code" != 0 ]; then
echo "$red_color"
else
echo "$normal_color"
fi
return "$exit_code"
PS1='[$(exit_color)][$?][$normal_color] w$ '
I would use the version with PROMPT_COMMAND
, just to save the subshell fork caused by the command substitution, but in practice the effect is minimal.
edited 28 mins ago
answered 35 mins ago
ilkkachu
51.2k678141
51.2k678141
add a comment |Â
add a comment |Â
Luke is a new contributor. Be nice, and check out our Code of Conduct.
Luke is a new contributor. Be nice, and check out our Code of Conduct.
Luke is a new contributor. Be nice, and check out our Code of Conduct.
Luke is a new contributor. Be nice, and check out our Code of Conduct.
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%2f470018%2fset-last-command-exit-code-to-bash-prompt%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
Welcome to U&L StackExchange! When you say "if I just do
$?
in my PS1...", how did you do that?â JigglyNaga
38 mins ago
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
36 mins ago
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
13 mins ago