3

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
0

1 Answer 1

8

This works:

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

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

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

2 Comments

Fantastic. Thank you for this! Where could I have looked this up? Googling wasn't helpful for a reference doc.
Go to the source: the GNU bash manual. In this case, the answer is in the parameter expansion section. Scroll down to read about ${parameter/pattern/string} and you'll find: "If parameter is an array variable subscripted with ‘@’ or ‘*’, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.