Bash escaping spaces in a file name, in a variable

I'm new to Bash, so it might be something trivial, but I just don't get it. I am trying to avoid spaces inside file names. Take a look. Note that this is a โ€œworking exampleโ€ - I get that interleaving files with blank pages can be made easier, but I'm here about space.

#! /bin/sh

first=true
i=combined.pdf
o=combined2.pdf
for f in test/*.pdf
do
    if $first; then
        first=false
        ifile=\"$f\"
    else
        ifile=$i\ \"$f\"
    fi
    pdftk $ifile blank.pdf cat output $o
    t=$i
    i=$o
    o=$t
    break
done

Say I have a file with a name my file.pdf(with a space). I want the ifile variable to contain a string combined.pdf "my file.pdf", so pdftk can use it as two file arguments: the first one is combined.pdfand the second is my file.pdf.

I tried various escaping methods (with or without first exiting the quotes themselves, etc.), but when pdftk is executed it preserves the splitting myand file.pdf.

EDIT: : ( ) pdftk. , , .

+4
2

. :

args=(combined.pdf "my file.pdf");

, "my file.pdf" .

:

pdftk "${args[@]}" ...

pdftk. "${args[@]}" , "" (.. , ).

, bash isms, , shebang

#!/bin/bash
+7

Try:

find test/*.pdf | xargs -I % pdftk % cat output all.pdf

, xargs - .

EDIT: , , , find - , ( - > string). , FP.

0

All Articles