Because brace expansion is performed before variable substitution, {1..$n}
will not work as one would like and there are good reasons to avoid eval
. A simple solution is to fix the filenames so that they sort in order.
Consider these files:
$ ls
page_10.jpg page_1.jpg page_2.jpg page_3.jpg page_4.jpg page_5.jpg page_6.jpg page_7.jpg page_8.jpg page_9.jpg
By prepending zeros to the single-digit file names, we can make them sort as you want:
$ for f in ./page_[0-9].jpg; do mv "$f" "./page_0${f#./page_}"; done
$ ls
page_01.jpg page_02.jpg page_03.jpg page_04.jpg page_05.jpg page_06.jpg page_07.jpg page_08.jpg page_09.jpg page_10.jpg
Now you can use:
convert *.jpg out.pdf
Converting 2 digits to 3
As smeterlink points out, the above can be extended to convert 2 digits to 3:
for f in ./page_[0-9][0-9].jpg; do mv "$f" "./page_0${f#./page_}"; done
As an example, and using the files as above:
$ for f in ./page_[0-9][0-9].jpg; do mv "$f" "./page_0${f#./page_}"; done
$ ls
page_001.jpg page_002.jpg page_003.jpg page_004.jpg page_005.jpg page_006.jpg page_007.jpg page_008.jpg page_009.jpg page_010.jpg