I want to define a variable representing the switches for some program to be run later. One of those switches needs a filename argument, where the filename may contain spaces or special characters. How do I do something like this properly?
#! /bin/bash
FILE="/home/user/Excludes (1).txt"
SWITCHES="--verbose --exclude $FILE" # both this and \"$FILE\" here fail.
someprogram $SWITCHES
Here is a real world example of trying to define a string in bash, that contains another string (though here the second one is an argument, rather than a switch):
$ cat > "foo bar"
me "foo bar"
$ FOOBAR="foo bar"
$ SWITCHES="-n $FOOBAR"
$ cat $SWITCHES
cat: foo: No such file or directory
cat: bar: No such file or directory
$ cat "$SWITCHES"
cat: invalid option -- ' '
Try 'cat --help' for more information.
$ SWITCHES="-n \"$FOOBAR\""
$ cat $SWITCHES
cat: '"foo': No such file or directory
cat: 'bar"': No such file or directory
$ cat "$SWITCHES"
cat: invalid option -- ' '
Try 'cat --help' for more information.