Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change values of bash array elements without loop

Tags:

arrays

bash

array=(a b c d) 

I would like to add a character before each element of the array in order to have this

array=(^a ^b ^c ^d) 

An easy way to do that is to loop on array elements and change values one by one

for i in "${#array[@]}" do     array[i]="^"array[i] done 

But I would like to know if there is any way to do the same thing without looping on the array as I have to do the same instruction on all elements.

Thanks in advance.

like image 869
saloua Avatar asked Oct 05 '12 10:10

saloua


People also ask

How do I change the value of an array element in bash?

To update element of an array in Bash, access the element using array variable and index, and assign a new value to this element using assignment operator.

How do you change the value of an element in an array?

To change the value of all elements in an array:Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.

How do you modify an array?

You click the formula in the cell or formula bar and you can't change a thing. Array formulas are a special case, so do one of the following: If you've entered a single-cell array formula, select the cell, press F2, make your changes, and then press Ctrl+Shift+Enter..

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.


2 Answers

Use Parameter Expansion:

array=("${array[@]/#/^}") 

From the documentation:

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

like image 76
choroba Avatar answered Oct 19 '22 03:10

choroba


This way also honor whitespaces in array values:

array=( "${array[@]/#/^}" ) 

Note, this will FAIL if array was empty and you set previously

set -u 

I don't know how to eliminate this issue using short code...

like image 22
socketpair Avatar answered Oct 19 '22 03:10

socketpair