diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh
index 3aaadfe37015002897a8a4e33a915212eb080bf5..4973f8fb4563baa05247d414a6462f9ef191449e 100755
--- a/dist/tools/openocd/openocd.sh
+++ b/dist/tools/openocd/openocd.sh
@@ -172,9 +172,12 @@ _split_banks() {
 
     # The following command needs specific osx handling (non gnu):
     # * Same commands for a pattern should be on different lines
+    # * Cannot use '\n' in the replacement string
+    local sed_escaped_newline=\\$'\n'
+
     sed -n '
     /^{.*}$/ {
-        s/\} /\}\n/g
+        s/\} /\}'"${sed_escaped_newline}"'/g
         s/[{}]//g
         p
     }'