help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Display control characters in the escaped form


From: Stephane Chazelas
Subject: Re: [Help-bash] Display control characters in the escaped form
Date: Wed, 2 Oct 2019 12:02:42 +0100
User-agent: NeoMutt/20171215

2019-10-01 22:24:35 -0500, Peng Yu:
> Hi,
> 
> The following bash code shows that declare -p does not print control
> characters in escaped form. Is it better to print them in the escaped
> form to make the results readable? Thanks.
> 
> $ IFS= read -r x <<< $'\001'
> $ declare -p x
> declare -- x=""
> $ xxd <<< "$x"
> 00000000: 010a                                     ..
[...]

You can always use printf %q or ${x@Q}

$ a=$'\1' bash -c 'typeset -p a; printf "%q\n" "$a"; printf
"%s\n" "${a@Q}"'
declare -x a=""
$'\001'
$'\001'

That doesn't help for all /control/ characters though. For
instance, with U+034F, the combining grapheme joiner which could
be considered a control character:

$ a=$'\u34f' bash -c 'typeset -p a; printf "%q\n" "$a"; printf "%s\n" "${a@Q}"'
declare -x a="͏"

'͏'

Note that in GNU locales, that character is classified as
"printable" and "graphic" (matches both [[:graph:]] and
[[:print:]]).

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]