| Daniel Rock 2006-10-24, 7:17 pm |
| jhagge@gmail.com wrote:
> I would guess most users will be saving this to there desktop, but if
> they did happen to put it in a directory with a space in the name, the
> variable is set wrong - basically stopping where the space is.
>
> If the path is ~/Desktop/Saved Files/DOGFOOD
>
> the variable gets set to:
>
> ~/Desktop/Saved
>
> Any ideas how to work around this?
Best practise: Don't use whitespace in filenames.
If you cannot avoid: Quote every variable. But multi-level quoting gets
very ugly:
dir=$(dirname "$0") # remember to quote("") $0
dir=$(cd "${dir}"; pwd) # again: quote ${dir}
# or in a single line:
dir=$(cd "$(dirname "$0")"; pwd)
# nesting quotes isn't always as easy as in this example
echo "${dir}" # quota again
filename="${dir}/some_file" # dito.
if [ -f "${filename}" ]; then
: do something
fi
Summary: Remember to quote any variable which could contain whitespace.
--
Daniel
|