I create bash scripts which get sourced from another "main" script to set up variables needed by the main script. These variables need to be able to contain any character and not have them interpreted by the shell. For example:
- a single quote: '
- dollar sign: $
- asterix: *
- pound sign: #, etc.
So, my thinking was to use a single quote and escape any enclosed single quotes and #
characters. But, am getting a unexpected EOF while looking for matching
error with the two files below.
Question:
What is the best way to define a string which can contain any set of characters that would require the least amount of tweaking?
There are thousands of such
foo.sh
files and as the string is being extracted form another source, I want to minimize the number of special characters that I need to escape.What other characters do I need to escape.
The desired output form the following scripts below is
\MyMacro{*,Baker's Dozen,$x^$,#}
Platform:
- MacOS 10.9.5
Sub Shell: foo.sh
set -f
string_list='*,Baker\'s Dozen,$x^$,\#'
set +f
Main Shell: main.sh
source foo.sh
printf "%s{%s}" "\MyMacro" "${string_list}"