Trying to use a variable in Single Quotes Bash -


i'm trying use variable $y in snippet below keep running filename showing $y.pdf

ls -v *.jpg | tr '\n' ' ' | sed 's/$/\ $y.pdf/' | xargs convert 

i've tried below:

ls -v *.jpg | tr '\n' ' ' | sed 's/$/\'"$y.pdf"'/' | xargs convert  ls -v *.jpg | tr '\n' ' ' | sed 's/$/\ {$y}.pdf/' | xargs convert 

the top 1 fails, sed expecting else. bottom 1 makes filename {$y}.pdf.

any ideas me?

thanks!

never mind quoting problem; wrong approach entirely. use for loop avoid kinds of potential problems.

for f in *.jpg;     convert "$f" "${f%.jpg}.pdf" done 

${f%.jpg} expands name of current file minus .jpg extension.

to merge files 1 pdf simpler (i think):

convert *.jpg "$y.pdf" 

assuming ls -v outputs file names in correct order, , there aren't of usual concerns parsing ls involved, use

ls -v *.jpg > input.txt convert @input.txt "$y.pdf" 

(there might way avoid use of tempfile, maybe simple ls -v *.jpg | convert @- "$y.pdf", i'm lazy figure out ways convert can called.)


Comments