0

I have a bash script with a line that was originally this

convert '%d.jpg[1-300]' combined.pdf

Uses convert from Imagemagick to strap a load of sequentially numbered jpgs in to a PDF.

I've written a basic script to accept user input for a changeable number of pages and tried to swap the 300 for a variable set by the user when the script runs using read -p to make this:

convert '%d.jpg[1-$pages]' combined.pdf

The rest of the script works fine but it fails on this line and I'm not sure why. Any suggestions?

I suspect its something to do with incorrect delimiters but I don't know the correct thing to look for.

5
  • Any chance to see what the files looks like? Better give us concrete examples, sample input, expected output Commented Mar 18, 2023 at 22:20
  • @GillesQuénot, see the Filename References section at imagemagick.org/script/command-line-processing.php#input for background. Commented Mar 18, 2023 at 22:38
  • @GillesQuénot the files are names 1.jpg 2.jpg 3.jpg etc. etc. a bunch of A4 page scans. they used to be a fixed size per pdf. But that goal post has moved and its now required to be flexible amount of pages.
    – Big_James
    Commented Mar 18, 2023 at 22:44
  • See: Difference between single and double quotes in bash
    – Cyrus
    Commented Mar 18, 2023 at 22:53
  • This is also possible: convert '%d.jpg[1-'"$pages"']' combined.pdf
    – Cyrus
    Commented Mar 18, 2023 at 22:54

1 Answer 1

1

Parameter expansion is not performed inside single-quotes. It is performed inside double quotes though, so:

convert "%d.jpg[1-$pages]" combined.pdf

In any case, you need some form of quoting as [ is special in most shells as a glob operator (fish being a notable exception, but in fish (older versions anyway) it's % that's special), and in all Bourne-like shells except zsh, unquoted parameter expansion is subject to split+glob which we don't want here.

1
  • That appears to have got it. Thank you for enlightening me as to what filename globbing and glob operators are and do. Same for parameter expansion.
    – Big_James
    Commented Mar 18, 2023 at 23:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.