Sample Header Ad - 728x90

bash mapfile NUL bug?

3 votes
1 answer
887 views
bash's mapfile seems to be broken when handling NUL separated input. In particular, it isn't handling minus characters (-) correctly, treating the empty string after one as an end-of-line marker. For example:
bash
#!/bin/bash

mkdir /tmp/junk
cd /tmp/junk

touch a.txt b.txt c.txt d-e-f.txt
declare -a files

echo "mapfile using default \\n delimiter"
mapfile -t  files  -d The  first  character  of delim is used to terminate each input line, rather than newline.  **If delim is the empty string, mapfile will terminate a line when it reads a NUL character**.


  

bash version is 5.1.8(1)-release:
none $ bash --version GNU bash, version 5.1.8(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
---

##### Update

Even weirder, it seems to be mangling existing elements of the array (as shown above) if it already exists, but not even creating it if it doesn't.
bash $ unset files $ mapfile -t -d'' files < <(find . -maxdepth 1 -type f -name '*.txt' -print0) $ typeset -p files -bash: typeset: files: not found ```
Asked by cas (82062 rep)
Jun 6, 2021, 09:28 AM
Last activity: Jul 3, 2022, 02:24 AM