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.

0

2 Answers 2

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

Output:

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

4 Comments

Please see update to clarify I do not want to append the array it is the array element
This adds something to the first array element. Take a look at: declare -p MY_ARR
@RixsonL MY_ARR+=( " bob" ) would append to the array, Cyrus appended to the element 0.
Thank you for clarifying David. Yes that is correct Cyrus thank you for your help.
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

2 Comments

Glad to help. Lot of smart bash folks here :)
It's quite scary sometimes :)

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.