help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] append to $@ one by one


From: Chet Ramey
Subject: Re: [Help-bash] append to $@ one by one
Date: Wed, 30 Oct 2019 11:26:09 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

On 10/21/19 1:53 PM, Jesse Hathaway wrote:
> On Sat, Oct 19, 2019 at 8:12 PM Peng Yu <address@hidden> wrote:
>> I use the following code to read from stdin and set $@ at once. But it
>> involves an additional variable "a". Is there a way to avoid such a
>> variable by appending to $@ one by one (but it should not be too slow,
>> so `set -- "$@" "$x"` will not work.)
>>
>> while IFS= read -r x; do
>>   a+=("$x")
>> done
>> set -- "${a[@]}"
> 
> It is an interesting question, from my brief reading of the source code it is
> presently not possible because the positional parameters are stored using a
> different data structure than standard bash variables:
> 
>   /* A linked list of words. */
>   typedef struct word_list {
>     struct word_list *next;
>     WORD_DESC *word;
>   } WORD_LIST;
> 
> Where as bash arrays are stored as:
> 
>   typedef struct array {
>     enum atype type;
>     arrayind_t max_index;
>     int num_elements;
>     struct array_element *lastref;
>     struct array_element *head;
>   } ARRAY;
> 
>   typedef struct array_element {
>     arrayind_t ind;
>     char *value;
>     struct array_element *next, *prev;
>   } ARRAY_ELEMENT;
> 
> There is quite a bit of special code that ensures operations such as `set` or
> `shift` on the positional parameters keep the $1..$9 variables intact.

This is true; the positional parameter data structure (a WORD_LIST *, which
is pervasive throughout the shell) predates arrays by many years.

> It looks like it would take quite a bit of work to allow appending items to 
> the
> argv positional parameters, but Chet would be able to speak from a position of
> authority on the matter.

I don't see a compelling reason to invent new syntax to do this. There are
already several reasonable ways to make it happen.


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://tiswww.cwru.edu/~chet/



reply via email to

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