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: Jesse Hathaway
Subject: Re: [Help-bash] append to $@ one by one
Date: Mon, 21 Oct 2019 12:53:38 -0500

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.

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.



reply via email to

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