I use this script (from here) in order to reduce the size of a pdf file:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \ -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

I have added this within a Dolphin action (service menu) with the line:

Exec=bash -c 'pdf=$(gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook    -sOutputFile=ebook.pdf "%u"); kdialog --title "Shrink" --msgbox "Done! $pdf";';

I would like the output file name to be automatically based on that of the input file, something like: input_xxx.

  • 1
    I'd try something like f="%u"; pdf=$(gs ... -sOutputFile="${f%.pdf}_xxx}" "$f"); ... Aug 25, 2021 at 16:33
  • @steeldriver - that's the answer. Could you post it as such? (Only, instead of "${f%.pdf}_xxx}", it should be "${f%.pdf}_xxx".)
    – cipricus
    Aug 25, 2021 at 22:50
  • Ooops sorry about that - done Aug 25, 2021 at 23:07
  • I meant "${f%.pdf}_xxx.pdf" for my specific script.
    – cipricus
    Aug 25, 2021 at 23:26

1 Answer 1


If you assign the %u value to a shell variable, it should be possible to use standard variable expansion syntax ${var%.ext}_xxx to remove the .pdf extension and add a suffix of your choice:

bash -c 'f="%u"; pdf=$(gs -dQUIET -dBATCH [pdf options] -sOutputFile="${f%.pdf}_xxx" "$f"); ...'

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.