help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fastest way to copy a bash array to a file


From: Grisha Levit
Subject: Re: fastest way to copy a bash array to a file
Date: Wed, 12 Jul 2023 23:48:50 -0400

On Wed, Jul 12, 2023, 23:15 Budi <budikusasi@gmail.com> wrote:

> How fastest, simplest way to copy or write a bash array values to a file
> e.g.
>
> m=( foo bar baz )
>
> printf '%s\n' "${m[@]}" >test
>

This is the way to go unless you have a pressing need to do something
different.

Is there/what'd be faster way by use of binary utility instead of
> redirection ?
>

It only matters in rather unusual cases, but it usually faster to build the
full string in a variable and write it all at once, rather than passing
each array element as a separate argument to printf:

$ a=({000000..999999})
$ time printf '%s\n' "${a[@]}" >x

real 0m1.865s
user 0m0.926s
sys 0m0.920s
$ time { IFS=$'\n'; o=${a[*]}; printf '%s\n' "$o" >y; }

real 0m0.261s
user 0m0.226s
sys 0m0.027s


reply via email to

[Prev in Thread] Current Thread [Next in Thread]