2

Given the following string, produced by jsawk:

[123,456,789]

Is there an idiomatic way of converting this into an array in BASH?

2 Answers 2

3

Strip the brackets, then use IFS to split on commas before populating the array with read.

foo="[123,456,789]"
IFS=, read -a list <<< "${foo:1:-1}"

This accommodates any comma-separated string.

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

Comments

1

You simply convert punctuations to spaces.

string='[123,456,789]'
array=(${string//[^0-9]/ })

Comments

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.