mainThe GNU Bourne-Again SHell - Support: sr #110820, mapfile/readfile assigning blanks...

 
 

sr #110820: mapfile/readfile assigning blanks and newlines

Submitter:  None
Submitted:  Sat 21 Jan 2023 05:33:07 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Invalid
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 24 Jan 2023 09:11:56 PM UTC, comment #2: 

Those are the correct results.

The values assigned to the array are

declare -a arr=([0]=$'a\n' [1]=$'b\n' [2]=$'c\n')

since you didn't tell mapfile to strip the delimiter (-t does that).

Those are the arguments that get passed to `echo'. `echo' does
what it does: display its arguments separated by spaces.

Since the arguments include newlines, the space echo uses to separate its arguments follows the newline.

Chet Ramey <chet>
Group administrator
Sat 21 Jan 2023 05:40:59 AM UTC, comment #1: 

Sorry, it should of course read mapfile/readarray - still early in the morning ... so also forgot to login - so added email-address now only.

Martin Anantharaman <manantharaman>
Sat 21 Jan 2023 05:33:07 AM UTC, original submission:  

If I do this ...
martin@MartinsAspire ~]$ mapfile arr
a
b
c
[martin@MartinsAspire ~]$ echo "${arr[@]}"
... I see ...
a
 b
 c

[martin@MartinsAspire ~]$
... explaining an error in an arcane shell-library I a developing.

Another thing: As part of debugging this I tried delimiting with null instead of newline - but that flops because in command-substitution (when I am e.g. building a data-set to input as a here-document) null-chars are dropped (at least with a warning about it).

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-24 chet StatusNone Invalid
    2023-01-21 manantharaman Carbon-CopyRemoved 130736 -

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code