]> code.delx.au - dotfiles/commitdiff
bashrc: use printf for compatibility with macOS's broken bash v3.x
authorJames Bunton <jamesbunton@delx.net.au>
Thu, 4 Apr 2019 03:03:19 +0000 (14:03 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Thu, 4 Apr 2019 03:03:19 +0000 (14:03 +1100)
.bashrc

diff --git a/.bashrc b/.bashrc
index 06b9d258bab6c90dd6108f3fe217142b3732b0cf..af9d7ff972f60649b3a090e8db77a7db773e4fcc 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -140,9 +140,10 @@ PS1="$PS1"'\n\$ '
 #################################
 
 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
@@ -292,31 +293,28 @@ fi
 
 # 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