Question

i have a problem in shell script

in a script i want to zip a list of pdf files

i have something like this working:

#!/usr/bin/bash


zip_path="/home/d/zz.zip"
pdf_list="/home/d/01.pdf /home/d/02.pdf"

zip ${zip_path} ${pdf_list}

i want to handle space in pdf path Something like that is working (with quote around ${pdf_list} at zip command) but it only works if pdf_list contain only 1 path:

#!/usr/bin/bash


zip_path="/home/d/zz.zip"
pdf_list="/home/d/0    1.pdf"

zip ${zip_path} "${pdf_list}"

so i tried something like this, but it does not work:

#!/usr/bin/bash


zip_path="/home/d/zz.zip"
pdf_list="\"/home/d/0    1.pdf\" \"/home/d/0    2.pdf\""

zip ${zip_path} ${pdf_list}

More generally the question is : With that command :

zip ${zip_path} ${pdf_list}

How do you set ${pdf_list} where it contains a list of file path (with space name) ?

Thanks

Was it helpful?

Solution

You'll have to use an array to handle several elements that contain whitespace:

pdf_list=( "/home/d/0    1.pdf" "/home/d/0    2.pdf" )

zip "$zip_path" "${pdf_list[@]}"
# ..............^^^^^^^^^^^^^^^^   this syntax is crucial: use exactly like this

You can use lots of whitespace to aid readability

pdf_list=(
    "/home/d/0    1.pdf" 
    "/home/d/0    2.pdf" 
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top