bug-bash
[Top][All Lists]
Advanced

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

Re: Fwd: Some incorrect behaviour for BASH arrays


From: Kerin Millar
Subject: Re: Fwd: Some incorrect behaviour for BASH arrays
Date: Fri, 1 Sep 2023 09:35:25 +0100

On Fri, 1 Sep 2023 14:44:49 +0700
Victor Pasko <victor.pasko@gmail.com> wrote:

> Just forward my response to all who was involved in discussion of my request
> 
> ---------- Forwarded message ---------
> From: Victor Pasko <victor.pasko@gmail.com>
> Date: Fri, Sep 1, 2023 at 2:23 PM
> Subject: Re: Some incorrect behaviour for BASH arrays
> To: Kerin Millar <kfm@plushkava.net>
> 
> 
> Thanks for the detailed explanations of *declare *.
> 
> As to the idea behind of my request:
> 1) I need local variable RESULT as empty string (not array)
> 2) this RESULT should collect symbols taken from other strings using
> ${STRING:i:1} for one symbol or ${STRING:i:k} for k-symbols
> So, the main question is: how to save such sub-strings in RESULT at needed
> j-place?
> With another words - I need RESULT as C-char-array to use it something like
> this
> 
> RESULT[j]="${STRING:i:1}"

You would have to reassemble RESULT from the appropriate substrings.

$ RESULT=cat; STRING=moo; i=1 j=1; 
RESULT=${RESULT:0:j}${STRING:i:1}${RESULT:j+1}; declare -p RESULT
declare -- RESULT="cot"

Alternatively, use an array then finish by joining its elements, using the [*] 
subscript.

f() {
    local RESULT=(c a t)
    local STRING=moo
    local IFS=
    local i=1 j=1
    RESULT[j]=${STRING:i:1}
    printf '%s\n' "${RESULT[*]}" # joins elements by first char of IFS (empty 
string)
}

-- 
Kerin Millar



reply via email to

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