In bash read builtin has -d for us to specify line delimiter other than newline

Does readarray provide some way to specify line delimiter?

  • Is it correct that it has no an option for that purpose?
  • Is there a shell default variable for that purpose, similar to IFS for field delimiter?

Thanks.


After I saw steeldriver's comment,

$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
    Read lines from a file into an array variable.

    A synonym for `mapfile'.

but later I discovered:

$ help mapfile
mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
    Read lines from the standard input into an indexed array variable.

    Read lines from the standard input into the indexed array variable ARRAY, or
    from file descriptor FD if the -u option is supplied.  The variable MAPFILE
    is the default ARRAY.

    Options:
      -d delim  Use DELIM to terminate lines, instead of newline
...
  • 1
    It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (-d delim) – steeldriver Nov 17 at 16:30
  • Thanks. Updated. – Tim Nov 17 at 16:32
  • 1
    @JeffSchaller "What happened when you tried?" – Tim Nov 17 at 16:39
up vote 3 down vote accepted

Since bash version 4.4, yes it does. From the release announcement Bash-4.4 Release Available:

The most notable new features are mapfile's ability to use an arbitrary record delimiter;

(readarray being a synonym for mapfile). The description in man bash is

          -d     The first character of delim is used  to  terminate  each
                 input line, rather than newline.
  • Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is 1 2 3. and readarray -d "2" myarr < f1 and echo "${myarr[0]}" outputs 1 2 and echo "${myarr[1]}" outputs 3. – Tim Nov 17 at 16:49
  • @Tim you can add -t if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter – steeldriver Nov 17 at 16:55
  • In 1 2 3, 2 isn't trailing however. What does "trailing" mean? – Tim Nov 17 at 17:07
  • 1
    @Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements 1 and 3 (the 1 would have a space after it and the 3 would be preceded by a space and probably followed by a newline). – Kusalananda Nov 17 at 17:36
  • 2
    @Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with -d) is included in the data unless -t is also used. See also When to use the terms "delimiter," "terminator," and "separator" – Kusalananda Nov 17 at 17:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.