3

I want to store verbatim text in Bash variables. I have an approach described here and I would like criticism and suggestions on improving this approach. Currently, the specific application is to have functions within a shell script library that are, to some extent, self documenting. Here is the sort of function I have in mind:

templateFunction(){
################################################################################
interrogationInformation=$(cat << 2012-09-26T1909
    <class>
        setup
    </class>
    <description>
        This is a natural language description of this function.
    </description>
    <prerequisiteFunctions>
        myFunction1
        myFunction2
    </prerequisiteFunctions>
    <prerequisitePrograms>
        myProgram1
        myProgram2
    </prerequisitePrograms>
2012-09-26T1909
)
################################################################################
if [ "${1}" != "-interrogate" ]; then #interrogation
    echo "function template"
fi #interrogation
}

An interrogation function can query the template function and the template function then returns some information about itself by way of the 'verbatim' variable interrogationInformation. The interrogation function parses this information.

In due course, it is likely that special characters such as quotation marks could be stored in these 'verbatim' variables (for example, in natural language). These variables could be used also for building code files, such as for web pages. The difficulty in using this approach is that cat can vary between distributions and the resulting behaviour can be unpredictable. Essentially, I would like criticism of my approach and suggestions for improvement. Perhaps one improvement would be a better (preferably standard) program than cat as used in this case.

I thank you very much for any suggestions.

d3pd
  • 7,935
  • 24
  • 76
  • 127
  • What's wrong with just `interrogationInformation=" ... ... ... "` ? – gniourf_gniourf Dec 04 '12 at 15:19
  • That would have the need of escaping lots of special characters. While this is certainly possible, it would be both inconvenient and would lack clarity in this case. The user should be able to both use the library and edit it quickly. You can see that pasting in lots of XML type text could quickly become tedious if escaping of characters were required. – d3pd Dec 04 '12 at 15:23
  • You'll only have to escape the dollar character (`$`) and the double quote character (`"`). You could also use single quotes (`'`) as: `interrogationInformation='... ... ...'` (but then you won't be able to use single quotes at all, not even escaped). Otherwise, dogbane's solution below is okay as it only relies on bash's builtin read. – gniourf_gniourf Dec 04 '12 at 15:28
  • There is also the option of ANSI-quoted strings in `bash`, i.e. `$'this is a string with \' embedded in the middle'`. They behave essentially like single-quoted strings, but do allow escaped single quotes (among other escaped characters) to be included. – chepner Dec 04 '12 at 18:40
  • Those all are functional answers, but given that it is likely for there to be lots of non shell code stored in the library and that functions in the library all shall have a similar form, I think dogbane's solution is the most appropriate one. Thanks muchly for your input. – d3pd Dec 08 '12 at 00:26

1 Answers1

7

You can use read instead of cat as follows:

IFS= read -d '' interrogationInformation << "EOF"
    <class>
        setup
    </class>
    <description>
        This is a natural language description of this function.
    </description>
    <prerequisiteFunctions>
        myFunction1
        myFunction2
    </prerequisiteFunctions>
    <prerequisitePrograms>
        myProgram1
        myProgram2
    </prerequisitePrograms>
EOF

Note that quoting EOF prevents any parameter expansion in the text.

dogbane
  • 266,786
  • 75
  • 396
  • 414