68

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");
}
1
  • 1
    Strings are immutable in Java, and don't have an append method. cannot find symbol symbol : method append(java.lang.String) location: class java.lang.String array[i].append (" content"); The simplified for-loop should slowly - after 5 years - be adopted too. for (String s: array) System.out.println (s + " content"); Commented Jun 21, 2011 at 14:38

4 Answers 4

153

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I was actually looking for prepending a string, so your # addition is very useful to me.
This, succinct and to the point, should be the accepted answer. It works with any string manipulation and applies the manipulation to each array element.
How about append the '_content' string to each dictionary keys?
what if prepend and append same time? /%/_content/#/prefix seems doesn't work.
@ZFY: you would need to perform two passes.
|
113

You can append a string to every array item even without looping in Bash!

# cf. http://codesnippets.joyent.com/posts/show/1826
array=(a b c d e)
array=( "${array[@]/%/_content}" )
printf '%s\n' "${array[@]}"

4 Comments

Good one! Is not the exact answer to the question, but is the unquestionable winner of the do-it-shorter competition! +1
The Joyent link is broken, see web.archive.org/web/20101114051536/http://… instead (Wayback Machine for that link).
Note: this does actually loop internally.
NB: If you have IFS=$'\n' (to handle names with whitespace) then the append command will not keep the original array structure. It will just make array contain only 1 item (a string list), with all the changed strings. As seen if you run: declare -p array afterwards.
18

Tested, and it works:

array=(a b c d e)
cnt=${#array[@]}
for ((i=0;i<cnt;i++)); do
    array[i]="${array[i]}$i"
    echo "${array[i]}"
done

produces:

a0
b1
c2
d3
e4

EDIT: declaration of the array could be shortened to

array=({a..e})

To help you understand arrays and their syntax in bash the reference is a good start. Also I recommend you bash-hackers explanation.

5 Comments

it works... but a bit confusing. can u explain what does "{}" mean, as in ${array[i]}? why not $array[$i]?
@Richard: unfortunately, the syntax required to work with bash arrays is ... arcane to put it mildly; I don't believe it can be explained easier than the man-page for bash does it. (Moreover, this serves as the 'stay-away sign' for me)
array[i]="${array[i]}$i" can be simplified to: array[$i]+="$i"
Thanks, this gives me idea to append string to specific element +1
...and this is the one that works for prefixes (prepending a string before the array elements), not just suffixes
2

You pass in the length of the array as the index for the assignment. The length is 1-based and the array is 0-based indexed, so by passing the length in you are telling bash to assign your value to the slot after the last one in the array. To get the length of an array, your use this ${array[@]} syntax.

declare -a array
array[${#array[@]}]=1
array[${#array[@]}]=2
array[${#array[@]}]=3
array[${#array[@]}]=4
array[${#array[@]}]=5
echo ${array[@]}

Produces

1 2 3 4 5

3 Comments

good answer. regrettably the required syntax (sin tax) can not be sufficiently lamented
Just reread the question and realized I answered something slightly different. I'll leave this answer here though since it still has some valuable information regarding arrays in bash.
array+=(1); array+=(2); ... array+=(etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.