BASH: Handling odd characters in a variable?
November 18, 2008 1:58 PM Subscribe
BASH Scripting. I'm trying to do a simple "mv" operation with a not-so-simple filename that contains special characters (as in brackets and hyphens). The filename itself is stored within a variable. The operation fails, and I can understand why, but I don't know how to get around it.
A sample filename would be something like:
[2008] Region {XX}.pdf
...and is stored in $sFilename. Not all of the filenames are the same format although all of them start with the year in square brackets.
I've got the rest of the script set up to parse for the year in the initial set of [ ] brackets and create a folder (./2008) with that name. The second part of the script is supposed to move the aforementioned file into the created folder. I attempt to do this with the command:
mv "$sFilename" ./$sPrefix
...where $sPrefix is the parsed item from the brackets. It errs out with the message:
mv: cannot stat `[2008]': No such file or directory.
It seems to me the shell is choking on the filename. What's the best way to work around this WITHOUT renaming the files (since the program that imports them is Windows-based and requires they be intact)?
Thanks
posted by Ziggy Zaga to computers & internet (11 answers total) 3 users marked this as a favorite
you can echo it and its fine, but then if you do the mv it doesn't work?
posted by alkupe at 2:05 PM on November 18, 2008