My two cents: I know `read` to be one of the worst way for reading
data, in term of performances. I know *shell* not to be able to
manage binary and mostly null bytes,

The most reliable way I use for parsing strings that contain
null bytes is mapfile (readarray):

  $ printf %b\\0 winter spring 'summer\0200apple\0200banana\0200cherry' automn|
       bash -c 'IFS= readarray -d "" -t arry;printf "<%q>\n" "${arry[@]}"'
  <winter>
  <spring>
  <$'summer\200apple\200banana\200cherry'>
  <automn>

or
  $  printf 'FOO\0\315\0\226\0\0BAR'
       bash -c 'IFS= readarray -d "" -t arry;printf "<%q>\n" "${arry[@]}"'
  <FOO>
  <$'\315'>
  <$'\226'>
  <''>
  <BAR>

Le Mon, Apr 21, 2025 at 07:48:30AM +0100, Stephane Chazelas a écrit :
> $ printf '%b\0' winter spring 'summer\0200apple\0200banana\0200cherry' automn 
> |
>    ./bash -c 'while IFS= read -rd "" season; do LC_ALL=C printf "<%q>\n" 
> "$season"; done'
> <winter>
> <spring>
> <summer>
> <apple>
> <banana>
> <cherry>
> <automn>
> 

-- 
 Félix Hauri  -  <fe...@f-hauri.ch>  -  http://www.f-hauri.ch

Reply via email to