#################################
function print_exit_code {
- local exit_msg="\e[01;33mexit code: $?\e[00m"
+ local exit_code="$?"
if [ -z "${BASH_SOURCE[1]}" ]; then
- echo -e "$exit_msg"
+ printf '\e[01;33mexit code: %s\e[00m\n' \
+ "$exit_code"
fi
}
trap print_exit_code ERR
# When at a prompt display `workingdir (hostname)`
function print_title_prompt {
- echo -ne "\e]0;bash:${PWD/$HOME/\~} ($(hostname -s))\a"
+ printf '\e]0;bash:%s (%s)\a' \
+ "${PWD/$HOME/\~}" \
+ "$(hostname -s)"
if [[ "$TERM" == screen* ]]; then
- echo -ne '\ekbash\e\\'
+ printf '\ekbash\e\\'
fi
}
PROMPT_COMMAND=print_title_prompt
-# Display the command about to be executed
-# This must go at the end of bashrc to avoid running the trap on commands in the bashrc
+# Display the command about to be executed. This must go at the end of the
+# bashrc to avoid running the trap on commands in the bashrc
function print_title_exec {
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return
- echo -ne '\e]0;'
- echo -n "$BASH_COMMAND" | tr -cd '[:graph:] '
- echo -n " ($(hostname -s))"
- echo -ne '\a'
+ printf '\e]0;%s (%s)\a' \
+ "$(tr -cd '[:graph:] ' <<< "$BASH_COMMAND")" \
+ "$(hostname -s)"
if [[ "$TERM" == screen* ]]; then
- echo -ne '\ek'
- echo -n "$BASH_COMMAND" | \
- sed 's/^sudo //' | \
- cut -d' ' -f1 | \
- tr -cd '[:graph:]'
- echo -ne '\e\\'
+ printf '\ek%s\e\\' \
+ "$(sed -n -e 's/sudo //' -e 's/ .*//' -e 1p <<< "$BASH_COMMAND")"
fi
}
trap print_title_exec DEBUG