0

Is there a way to append an Array element in bash? For Example:

$ declare -a MY_ARR=('Bob' 'Jim Jim' 'Dug Dug' 'Mark Mark')
$ echo "${MY_ARR[0]}"
Bob
$ MY_ARR[0]<< "bob"
$ echo "${MY_ARR[0]}"
Bob bob

I'm pretty sure that there is a relatively simple way of doing this in PHP but I can't seem to find a bash solution.

Just to clarify I want to add something to the current array element not overwrite it.

EDIT: I want to append a current Array ELEMENT not add another element to the array. It is the actual array ELEMENT contents that I want to edit.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
RixsonL
  • 89
  • 1
  • 1
  • 7

2 Answers2

1
declare -a MY_ARR=('Bob' 'Jim Jim' 'Dug Dug' 'Mark Mark')
MY_ARR[0]+=" bob"
echo "${MY_ARR[0]}"

Output:

Bob bob
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1
$ declare -a MY_ARR=('Bob' 'Jim Jim' 'Dug Dug' 'Mark Mark')
$ echo "${MY_ARR[0]}"
bob
$ MY_ARR[0]="${MY_ARR[0]} bob"
$ echo "${MY_ARR[0]}"
bob bob
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85