Thread: mapfile bash builtin

  1. #1
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808

    mapfile bash builtin

    The bash builtin mapfile reads from standard input and copies lines to the given array (the -t option removes the newline).

    I can't figure out why it works in the first case below but not the second:
    Code:
    $ echo -e "one\ntwo\nthree" > tempfile; mapfile -t an_array1 < tempfile
    $ echo ${an_array1[1]}
    two
    
    
    $ echo -e "one\ntwo\nthree" | mapfile -t an_array2
    $ echo ${an_array2[1]}
             <-- doesn't print anything
    $ echo ${#an_array2[*]}   # print array length
    0
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    bug-bash-gnu - Re: Data piped to 'mapfile', 'readarray', 'read -a' shell builtins is ignored - msg#00087 - OSDir.com
    Looks like it's due to the fact that the pipe operator uses subshells for each of the commands in the pipeline, and those processes don't propagate their shell variables (like an_array2) up to the parent process (the shell that called your piped commands).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. builtin function for getting current directory
    By nicoeschpiko in forum C Programming
    Replies: 4
    Last Post: 05-07-2010, 03:18 PM
  2. Can't run Bash!
    By Yarin in forum Tech Board
    Replies: 3
    Last Post: 05-11-2009, 07:25 PM
  3. Windows Version of bash's 'time' builtin
    By QuestionC in forum Windows Programming
    Replies: 6
    Last Post: 11-21-2006, 06:28 PM
  4. Mapfile for Isometric Tile maps
    By Wraithan in forum Game Programming
    Replies: 12
    Last Post: 09-29-2006, 12:48 AM
  5. builtin command
    By jon21 in forum Linux Programming
    Replies: 1
    Last Post: 04-11-2002, 03:42 AM