I've been attempting to simplify installation of a set of scripts by nesting them within a larger script using here documents (so I can still edit them like normal scripts). However, although nothing within the here document is supposed to be interpreted, my shell seems to stumble on mismatched closing brackets, such as can be found in a case statement.
For example:
#!/bin/sh
script=$(cat <<- 'NESTED_SCRIPT'
case "$1" in
foo)
echo "Foo"
;;
bar)
echo "Bar"
;;
esac
NESTED_SCRIPT
)
The above will fail with a syntax error on line 6 unless backslashes are placed before the closing brackets within the case
statement. It's like for some reason the shell isn't waiting for the special here document termination, but is terminating as soon as it finds the first closing bracket that has no corresponding opening bracket. Unfortunately escaping the brackets isn't an option as this leaves me with a script that needs to have the backslashes stripped from it; although I could do that I suppose I'd rather avoid it if I can.
Anyway, I know it's a bit of a weird use-case, but what I can't understand is why it's terminating early like this. Is there a way that I can force it to only terminate with the NESTED_SCRIPT
sequence without interpreting brackets at all, or some other workaround I can use? Unfortunately I need to assign the script to a variable due to the way I'm working with it (using eval
).