2

I have the following string that I read in from a file in bash

BOB_ENV: DEV\nStorage: @MOM.KV(Name=lol-lol;SCT=c-s-string)\nConn__name: ab-cb-ac-one.sb.w.net\nTest: Test

I am trying to make the \n the delimiter and store into an array

I am not sure if I am doing it properly but I have the following script

test="BOB_ENV: DEV\nStorage: @MOM.KV(Name=lol-lol;SCT=c-s-string)\nConn__name: ab-cb-ac-one.sb.w.net\nTest: Test"
arr=(`echo $test`)
echo ${arr[1]}

right now it splits the string on the spaces I have in the string and stores into the array. I want to split on the \n in the string.

1 Answer 1

2

You can use printf '%b' to interpret \n as line break and read it into array using readarray:

s='BOB_ENV: DEV\nStorage: @MOM.KV(Name=lol-lol;SCT=c-s-string)\nConn__name: ab-cb-ac-one.sb.w.net\nTest: Test'

# read into array
readarray -t arr < <(printf '%b\n' "$s")

# check array content
declare -p arr

Output:

declare -a arr=([0]="BOB_ENV: DEV" [1]="Storage: @MOM.KV(Name=lol-lol;SCT=c-s-string)" [2]="Conn__name: ab-cb-ac-one.sb.w.net" [3]="Test: Test")

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

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.