1

I have a long line that comes as output from a git command: a=$(git submodule foreach git status). It looks like this: a = "Entering 'Dir1/Subdir' On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean Entering 'Dir2' HEAD detached at xxxxxx nothing to commit, working tree clean Entering 'Dir3' On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean Entering 'Dir4' On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean"

I want to separate it into an array:

ARR[0] = "'Dir1/Subdir' On branch master ..."

ARR[1] = "'Dir2' HEAD detached at ..."

etc.

To do that, I have tried to substitute "Entering " for a symbol (I have tried # $ % & \t ...) with a=${a//Entering /$} and it works alright. Then, I try to use IFS and read to separate it into an array: IFS='$' read -ra ARR <<< "$a"

It's here where I am facing problems.

The output that I get of echo ${ARR[@]} is "Dir1/Subdir1" so I think that read is being affected by spaces or by how the output from git is, but I don't understand what is happening and how to fix it. Could you please give me any suggestions?

Thank you.

0

1 Answer 1

1

You can use readarray bash builtin and specify the delimiter within the same command:

readarray -d 'char delimiter' array <<< $variable

For example:

readarray -d '@' array <<< ${a//Entering /@}

Finally when you print each result you might want to remove the @ (or any other character used as delimiter):

echo ${array[1]%@}
echo ${array[2]%@}
echo ${array[@]%@}

If you want to delete the index 0 (because it contains @) you can reassign the array by copying the items from index 1 to last index:

array=("${array[@]:1}")

Tip: If you want to avoid use ${array[index]%@} each time you want to get some item, you can reassign the array again by removing the @ with:

array=("${array[@]/@}")
6
  • This worked really well! The only problem I had was to get rid of the first element, but I will just unset it. Thank you very much!
    – Zaida
    Sep 27, 2022 at 21:32
  • 1
    @Zaida glad that worked for you. I updated my answer to provide a possible solution to remove the index 0 and avoid use bash parameter expansion. Sep 27, 2022 at 22:23
  • Hi Edgar, the removal of index 0 worked flawlessly, but the removal of the delimiter worked with @ as delimiter but not with %. Do you know what could be happening?
    – Zaida
    Sep 28, 2022 at 5:11
  • 1
    @Zaida great you have solved ! About your question about what is happening I think it's because the bash parameter expansion uses special characters like % (e.g. echo ${array[1]%@}) or # to apply pattern and get a result. See this article or you can use man bash Sep 28, 2022 at 5:25
  • 1
    Another possible solution is using array=("${array[@]//%}"). I'm not really sure if this code and your do exactly the same but both work. Sep 28, 2022 at 5:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .