14 of 14 Added language tag for better colors.

There are a lot of ways to do this. The most portable two that I know of are sed and od - they're both POSIX.

printf '\n\r\b\t\033[01;31m' | sed -n l

It does like... read style escapes - C-style.



od is a little more configurable...

printf '\n\r\b\t\033[01;31m' |
od -v -w12 -t c -t a -A n

 \n  \r  \b  \t 033   [   0   1   ;   3   1   m
 nl  cr  bs  ht esc   [   0   1   ;   3   1   m

If you wanna know what all of those options do you can look in man od, but I specify I want two types of escapes - the -t c backslash escapes and the -t a named characters. The -w option used above is not POSIX-specified.

And here's a little shell function that will portably print out the octal values of each byte in its arguments - which, of course, od might handle as well with -t o:

proctal() (LC_ALL=C
    for a do while [ -n "$a" ] 
    do printf %o\\n "'$a"
    a=${a#?}; done; done)  

That's a simple one. This is a little more complicated. It should be able to do what the shell-specific printf -q implementations can, though.

bsq() (set -f; export LC_ALL=C IFS=\'
    for a do q=${a##*\'}; printf \'
    [ -n "${a#"$q"}" ] &&
        printf "%s'\''" ${a%\'*}
    printf "%s'\n'''''\n" "$q"; done |
    sed -n "/'''''"'/!H;1h;//!d;$!n;x;l' |
    sed -e :n -e '/\\$/N;s/.\n//;tn

Using out example string from earlier with a little additional:

bsq "$(printf '\n\r\'\''b\t\033[01;31m')"



It's only slightly different. You might notice there's an extra 0 and an extra \backslash. This is to allow for easy translation to a read or a %b printf argument. For example:

until [ $((i=$i+1)) -gt 5 ]
do touch "\%$i$(printf 'hey\b \t;\n\033 ')"
done   #just for ugly's sake

bsq * | eval "
    printf '<%b>\n' $(tr \\n \ )
" | tee /dev/fd/2 |
sed -n l


<\%1he  ;
<\%2he  ;
<\%3he  ;
<\%4he  ;
<\%5he  ;
<\\%1hey\b \t;$
\033 >$
<\\%2hey\b \t;$
\033 >$
<\\%3hey\b \t;$
\033 >$
<\\%4hey\b \t;$
\033 >$
<\\%5hey\b \t;$
\033 >$