Prompted (pun intended) by the recent thread on detecting missing newlines in command output, I'd had another look at my own version, and discovered a potential issue with control characters being written as-is in declare output. Minimal (harmless) reproducer:
╶➤ x () { echo $'\e[31m'"oops"$'\e[0m'; }
╶➤ declare -f x
x ()
{
    echo ''"oops"''
}

Emits the string in red in a terminal. Any instances with control sequences that do anything more invasive with the terminal cause more trouble.
Similarly:

╶➤ y=$'\e[31m'"oops"$'\e[0m'

╶➤ declare -p y
declare -- y="oops"

String again in red.  Compare with:

╶➤ set |grep ^y=
y=$'\E[31moops\E[0m'

No issues with the set output. As they can both be usefully re-read by the shell, is there any possibility for similarly escaped output from the declare builtin, instead of raw control characters?
-Rob

Reply via email to