I have an array in Bash, each element is a string. How can I append another string to each element? In Java, the code is something like:
for (int i=0; i<array.length; i++) { array[i].append("content"); }
To append element(s) to an array in Bash, use += operator. This operator takes array as left operand and the element(s) as right operand. The element(s) must be enclosed in parenthesis. We can specify one or more elements in the parenthesis to append to the given array.
The += Operator in Bash Bash is a widely used shell in Linux, and it supports the '+=' operator to concatenate two variables. As the example above shows, in Bash, we can easily use the += operator to concatenate string variables.
As mentioned by hal
array=( "${array[@]/%/_content}" )
will append the '_content' string to each element.
array=( "${array[@]/#/prefix_}" )
will prepend 'prefix_' string to each element
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With