0

I found the following example from here. But I can't understand how is array arr is being defined.

a='domain.de;de;https'
$ arr=(${a//;/ })

What is the advantage of defining it like this?

Actually, I want to store an array inside an array of varying size like as follows:

declare -a Workspace=(
    "${Folder[0]}"  "CFD"   "General,Markdown"
    "${Folder[4]}"  "GPU"   "General,Markdown,Python,C,Java"
)

For example in above, I want to access the terms General and Markdown for CFD.

  • 1
    Just because you can make an array of space delimited elements in bash doesn't make it a good idea. A good fat structure is important, and if the problem is this complex, consider a more powerful and flexible language like Perl. – Jeff Schaller Nov 22 '18 at 0:44
1

An array of arrays is a bad idea in shell (any shell). You need some other language.

how is array arr being defined.?

a='domain.de;de;https'
arr=(${a//;/ })

It works by:

  • replacing every ; by an space
  • Assuming IFS is space, tab, newline (the default)
  • splitting on space (included in IFS) the unquoted expansion of ${...}
  • assigning it to an array (...)
  • and naming that array arr=.

What is the advantage of defining it like this?

None, only problems:

  • If any of the elements contains an space, a tab or a newline, it will be split.
  • As globbing was not turn off, any *, ? or [ ] will be expanded to matching files.
  • If nullglob is active, any string containing *,? or [ ] will be removed.
  • if failglob is active any of the previous characters will generate an error.

In short, splitting on the shell is full of gotchas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.