Or,… or an introductory guide to robust filename handling and other string passing in shell scripts.
I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).
I encountered a problem such as the following:
- I have a file name containing a space
hello world, and it was treated as two separate fileshelloandworld. - I have an input line with two consecutive spaces and they shrank to one in the input.
- Leading and trailing whitespace disappears from input lines.
- Sometimes, when the input contains one of the characters
\[*?, they are replaced replaced by some text which is is actually the namenames of some files. - There is an apostrophe
'(or a double quote") in the input, and things got weird after that point. - There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style
\separators).
What is going on, and how do I fix itthis?