Search code examples
bashescapingquotes

How do I deal with unusual filenames in a command's parameters?


I have a file ('list') which contains a large list of filenames, with all kinds of character combinations such as:

-sensei

I am using the following script to process this list of files:

#!/bin/bash
while read -r line
do
    html2text -o ./text/$line $line
done < list

Which is giving me 'Cannot open input file' errors. What is the correct way of dealing with these filenames, to prevent any errors?

I have changed the example list above to now include only one filename (out of many) which does not work, no matter how I quote or don't quote it.

#!/bin/bash
while read -r line
do
    html2text -o "./text/$line" "$line"
done < list

The error I get is:

Unrecognized command line option "-sensei", try "-help".

As such this question does not resolve this issue.


Solution

  • Something like this should fix your issues (unless the file list has CRLF line endings):

    while IFS='' read -r file
    do
        html2text -o ./text/"$file" -- "$file"
    done < filelist.txt
    

    notes:

    • IFS='' read -r is mandatory when you want to capture a line accurately
    • most commands support -- for signaling the end of options; whatever the following arguments might be, they will not be treated as options. BTW, an other common work-around for filenames that start with - is to prepend ./ to them.