Search code examples
arraysbashprincexml

Expanding an array in BASH


I have an array:

allChapters=('1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I')

I need to run a command that takes specifying parameters like:

prince index.html 1.html 2.html 3.html 4.html 5.html 6.html 7.html 8.html 9.html 10.html 11.html 12.html 13.html 14.html A.html B.html C.html D.html E.html F.html G.html H.html I.html  -o doc.pdf

Is there a way to expand an array without manually typing it all so I can just add to the array and not have to modify the command?

Something like:

prince ${allChapters[*]}.html -o doc.pdf

Solution

  • This works:

    prince "${allChapters[@]/%/.html}" -o doc.pdf
    

    That replaces the empty string at the end of each entry with the desired extension.