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()
git branch 2> /dev/null
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
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()
git branch 2> /dev/null
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
Welcome to U&L StackExchange! When you say "if I just do$?
in my PS1...", how did you do that?
â JigglyNaga
Sep 19 at 13:33
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
Sep 19 at 13:34
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
Sep 19 at 13:58
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()
git branch 2> /dev/null
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
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()
git branch 2> /dev/null
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
edited Sep 19 at 13:33
Goro
6,16652762
6,16652762
asked Sep 19 at 13:26
Luke
112
112
Welcome to U&L StackExchange! When you say "if I just do$?
in my PS1...", how did you do that?
â JigglyNaga
Sep 19 at 13:33
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
Sep 19 at 13:34
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
Sep 19 at 13:58
add a comment |Â
Welcome to U&L StackExchange! When you say "if I just do$?
in my PS1...", how did you do that?
â JigglyNaga
Sep 19 at 13:33
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
Sep 19 at 13:34
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
Sep 19 at 13:58
Welcome to U&L StackExchange! When you say "if I just do
$?
in my PS1...", how did you do that?â JigglyNaga
Sep 19 at 13:33
Welcome to U&L StackExchange! When you say "if I just do
$?
in my PS1...", how did you do that?â JigglyNaga
Sep 19 at 13:33
2
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
Sep 19 at 13:34
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
Sep 19 at 13:34
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
Sep 19 at 13:58
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
Sep 19 at 13:58
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
7
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: Bash interprets them before other 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
1
down vote
I like to have a 3 line prompt, so I do this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:56:52
jackman@jackmanVM:~/tmp
+$ cat ~/.bash_prompt
# ... some other stuff ...
__bash_prompt()
local last_status=$1
local cwd=$( sed "s,^$HOME,~," <<<"$PWD" )
local user_host_path="$debian_chroot:+($debian_chroot) $(id -un)@$(hostname -s):$cwd"
# terminal title
echo -ne "e]0;$user_host_patha"
# separator and date
local char="ð"
printf "%s bash %s " "$(jot -s "" -b "$char" $(( $(tput cols) - 18 )) )" "$char$char"
date '+%T'
if ((last_status != 0)); then
local color_bold='e[0;1m'
local color_reset='e[0m'
printf "$color_bold[%d]$color_reset " $last_status
fi
# user@host, directory, git branch
printf "%s%sn" "$user_host_path$(git_current_branch " (%s)")"
# ref: https://stackoverflow.com/questions/1039713/different-bash-prompt-for-different-vi-editing-mode
# a single-line PS1 allows the show-mode-in-prompt inputrc setting to be useful
PROMPT_COMMAND='__bash_prompt $?'
PS1='$ '
I source that file in my ~/.bashrc
The exit status shows up like this
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:10
jackman@jackmanVM:~/tmp
+$ sh -c 'exit 42'
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:14
[42] jackman@jackmanVM:~/tmp
+$ false
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:16
[1] jackman@jackmanVM:~/tmp
+$ true
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:17
jackman@jackmanVM:~/tmp
+$
Note that the first thing I do in the function is to stash the last exit status, before I execute any other commands
I use the PROMPT_COMMAND to output all the fancy stuff and have just a simple PS1 because I use vi mode and I like to see the vi mode indicator. I also have this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:57:21
jackman@jackmanVM:~/tmp
+$ cat ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on
$if Bash
# not until bash 4.4, I believe
# escape sequences: https://stackoverflow.com/a/42107711/7552
#set vi-ins-mode-string "+1e[5 q2"
#set vi-cmd-mode-string ":1e[1 q2"
set vi-ins-mode-string +
set vi-cmd-mode-string :
$endif
I use fish as my login shell, and occassionally drop into ksh, so I put the
name of the shell in the prompt separator so I can keep track of what I'm typing.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
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: Bash interprets them before other 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
7
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: Bash interprets them before other 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
7
down vote
up vote
7
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: Bash interprets them before other 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: Bash interprets them before other 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 Sep 19 at 14:24
answered Sep 19 at 13:36
ilkkachu
52.3k679144
52.3k679144
add a comment |Â
add a comment |Â
up vote
1
down vote
I like to have a 3 line prompt, so I do this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:56:52
jackman@jackmanVM:~/tmp
+$ cat ~/.bash_prompt
# ... some other stuff ...
__bash_prompt()
local last_status=$1
local cwd=$( sed "s,^$HOME,~," <<<"$PWD" )
local user_host_path="$debian_chroot:+($debian_chroot) $(id -un)@$(hostname -s):$cwd"
# terminal title
echo -ne "e]0;$user_host_patha"
# separator and date
local char="ð"
printf "%s bash %s " "$(jot -s "" -b "$char" $(( $(tput cols) - 18 )) )" "$char$char"
date '+%T'
if ((last_status != 0)); then
local color_bold='e[0;1m'
local color_reset='e[0m'
printf "$color_bold[%d]$color_reset " $last_status
fi
# user@host, directory, git branch
printf "%s%sn" "$user_host_path$(git_current_branch " (%s)")"
# ref: https://stackoverflow.com/questions/1039713/different-bash-prompt-for-different-vi-editing-mode
# a single-line PS1 allows the show-mode-in-prompt inputrc setting to be useful
PROMPT_COMMAND='__bash_prompt $?'
PS1='$ '
I source that file in my ~/.bashrc
The exit status shows up like this
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:10
jackman@jackmanVM:~/tmp
+$ sh -c 'exit 42'
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:14
[42] jackman@jackmanVM:~/tmp
+$ false
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:16
[1] jackman@jackmanVM:~/tmp
+$ true
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:17
jackman@jackmanVM:~/tmp
+$
Note that the first thing I do in the function is to stash the last exit status, before I execute any other commands
I use the PROMPT_COMMAND to output all the fancy stuff and have just a simple PS1 because I use vi mode and I like to see the vi mode indicator. I also have this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:57:21
jackman@jackmanVM:~/tmp
+$ cat ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on
$if Bash
# not until bash 4.4, I believe
# escape sequences: https://stackoverflow.com/a/42107711/7552
#set vi-ins-mode-string "+1e[5 q2"
#set vi-cmd-mode-string ":1e[1 q2"
set vi-ins-mode-string +
set vi-cmd-mode-string :
$endif
I use fish as my login shell, and occassionally drop into ksh, so I put the
name of the shell in the prompt separator so I can keep track of what I'm typing.
add a comment |Â
up vote
1
down vote
I like to have a 3 line prompt, so I do this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:56:52
jackman@jackmanVM:~/tmp
+$ cat ~/.bash_prompt
# ... some other stuff ...
__bash_prompt()
local last_status=$1
local cwd=$( sed "s,^$HOME,~," <<<"$PWD" )
local user_host_path="$debian_chroot:+($debian_chroot) $(id -un)@$(hostname -s):$cwd"
# terminal title
echo -ne "e]0;$user_host_patha"
# separator and date
local char="ð"
printf "%s bash %s " "$(jot -s "" -b "$char" $(( $(tput cols) - 18 )) )" "$char$char"
date '+%T'
if ((last_status != 0)); then
local color_bold='e[0;1m'
local color_reset='e[0m'
printf "$color_bold[%d]$color_reset " $last_status
fi
# user@host, directory, git branch
printf "%s%sn" "$user_host_path$(git_current_branch " (%s)")"
# ref: https://stackoverflow.com/questions/1039713/different-bash-prompt-for-different-vi-editing-mode
# a single-line PS1 allows the show-mode-in-prompt inputrc setting to be useful
PROMPT_COMMAND='__bash_prompt $?'
PS1='$ '
I source that file in my ~/.bashrc
The exit status shows up like this
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:10
jackman@jackmanVM:~/tmp
+$ sh -c 'exit 42'
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:14
[42] jackman@jackmanVM:~/tmp
+$ false
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:16
[1] jackman@jackmanVM:~/tmp
+$ true
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:17
jackman@jackmanVM:~/tmp
+$
Note that the first thing I do in the function is to stash the last exit status, before I execute any other commands
I use the PROMPT_COMMAND to output all the fancy stuff and have just a simple PS1 because I use vi mode and I like to see the vi mode indicator. I also have this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:57:21
jackman@jackmanVM:~/tmp
+$ cat ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on
$if Bash
# not until bash 4.4, I believe
# escape sequences: https://stackoverflow.com/a/42107711/7552
#set vi-ins-mode-string "+1e[5 q2"
#set vi-cmd-mode-string ":1e[1 q2"
set vi-ins-mode-string +
set vi-cmd-mode-string :
$endif
I use fish as my login shell, and occassionally drop into ksh, so I put the
name of the shell in the prompt separator so I can keep track of what I'm typing.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I like to have a 3 line prompt, so I do this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:56:52
jackman@jackmanVM:~/tmp
+$ cat ~/.bash_prompt
# ... some other stuff ...
__bash_prompt()
local last_status=$1
local cwd=$( sed "s,^$HOME,~," <<<"$PWD" )
local user_host_path="$debian_chroot:+($debian_chroot) $(id -un)@$(hostname -s):$cwd"
# terminal title
echo -ne "e]0;$user_host_patha"
# separator and date
local char="ð"
printf "%s bash %s " "$(jot -s "" -b "$char" $(( $(tput cols) - 18 )) )" "$char$char"
date '+%T'
if ((last_status != 0)); then
local color_bold='e[0;1m'
local color_reset='e[0m'
printf "$color_bold[%d]$color_reset " $last_status
fi
# user@host, directory, git branch
printf "%s%sn" "$user_host_path$(git_current_branch " (%s)")"
# ref: https://stackoverflow.com/questions/1039713/different-bash-prompt-for-different-vi-editing-mode
# a single-line PS1 allows the show-mode-in-prompt inputrc setting to be useful
PROMPT_COMMAND='__bash_prompt $?'
PS1='$ '
I source that file in my ~/.bashrc
The exit status shows up like this
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:10
jackman@jackmanVM:~/tmp
+$ sh -c 'exit 42'
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:14
[42] jackman@jackmanVM:~/tmp
+$ false
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:16
[1] jackman@jackmanVM:~/tmp
+$ true
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:17
jackman@jackmanVM:~/tmp
+$
Note that the first thing I do in the function is to stash the last exit status, before I execute any other commands
I use the PROMPT_COMMAND to output all the fancy stuff and have just a simple PS1 because I use vi mode and I like to see the vi mode indicator. I also have this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:57:21
jackman@jackmanVM:~/tmp
+$ cat ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on
$if Bash
# not until bash 4.4, I believe
# escape sequences: https://stackoverflow.com/a/42107711/7552
#set vi-ins-mode-string "+1e[5 q2"
#set vi-cmd-mode-string ":1e[1 q2"
set vi-ins-mode-string +
set vi-cmd-mode-string :
$endif
I use fish as my login shell, and occassionally drop into ksh, so I put the
name of the shell in the prompt separator so I can keep track of what I'm typing.
I like to have a 3 line prompt, so I do this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:56:52
jackman@jackmanVM:~/tmp
+$ cat ~/.bash_prompt
# ... some other stuff ...
__bash_prompt()
local last_status=$1
local cwd=$( sed "s,^$HOME,~," <<<"$PWD" )
local user_host_path="$debian_chroot:+($debian_chroot) $(id -un)@$(hostname -s):$cwd"
# terminal title
echo -ne "e]0;$user_host_patha"
# separator and date
local char="ð"
printf "%s bash %s " "$(jot -s "" -b "$char" $(( $(tput cols) - 18 )) )" "$char$char"
date '+%T'
if ((last_status != 0)); then
local color_bold='e[0;1m'
local color_reset='e[0m'
printf "$color_bold[%d]$color_reset " $last_status
fi
# user@host, directory, git branch
printf "%s%sn" "$user_host_path$(git_current_branch " (%s)")"
# ref: https://stackoverflow.com/questions/1039713/different-bash-prompt-for-different-vi-editing-mode
# a single-line PS1 allows the show-mode-in-prompt inputrc setting to be useful
PROMPT_COMMAND='__bash_prompt $?'
PS1='$ '
I source that file in my ~/.bashrc
The exit status shows up like this
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:10
jackman@jackmanVM:~/tmp
+$ sh -c 'exit 42'
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:14
[42] jackman@jackmanVM:~/tmp
+$ false
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:16
[1] jackman@jackmanVM:~/tmp
+$ true
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 11:05:17
jackman@jackmanVM:~/tmp
+$
Note that the first thing I do in the function is to stash the last exit status, before I execute any other commands
I use the PROMPT_COMMAND to output all the fancy stuff and have just a simple PS1 because I use vi mode and I like to see the vi mode indicator. I also have this:
ððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð bash ðð 10:57:21
jackman@jackmanVM:~/tmp
+$ cat ~/.inputrc
set editing-mode vi
set show-mode-in-prompt on
$if Bash
# not until bash 4.4, I believe
# escape sequences: https://stackoverflow.com/a/42107711/7552
#set vi-ins-mode-string "+1e[5 q2"
#set vi-cmd-mode-string ":1e[1 q2"
set vi-ins-mode-string +
set vi-cmd-mode-string :
$endif
I use fish as my login shell, and occassionally drop into ksh, so I put the
name of the shell in the prompt separator so I can keep track of what I'm typing.
answered Sep 19 at 15:07
glenn jackman
48.3k365105
48.3k365105
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%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
Sep 19 at 13:33
2
Linking in the already-related: unix.stackexchange.com/questions/8396/â¦
â Jeff Schaller
Sep 19 at 13:34
@JeffSchaller, urgh, the accepted answer there is just wrong
â ilkkachu
Sep 19 at 13:58