Sample Header Ad - 728x90

Creating and appending to an array, mapfile vs arr+=(input) same thing or am I missing something?

1 vote
1 answer
890 views
Is there a case where mapfile has benefits over arr+=(input)? ## Simple examples ### mapfile array name, arr:
mkdir {1,2,3}

mapfile -t arr < <(ls)

declare -p arr
### output:
declare -a arr=()="1" ="2" ="3")
Edit: changed title for below; the body had y as the array name, but the title had arr as the name, which this could lead to confusion. ## y+=(input)
IFS=$'\n'

y+=($(ls))

declare -p y
### output:
declare -a y=()="1" ="2" ="3")
--- An advantage to mapfile is you don't have to worry about word splitting I think. For the other way you can avoid word splitting by setting IFS=$'\n' although for this example it's nothing to worry about. The second example just seems easier to write, anything I'm missing out on?
Asked by Nickotine (554 rep)
Jun 25, 2023, 01:23 AM
Last activity: Jan 31, 2025, 02:29 PM