存储在Bash变量中的逐字文本

存储在Bash变量中的逐字文本,bash,shell,variables,cat,verbatim,Bash,Shell,Variables,Cat,Verbatim,我想在Bash变量中存储逐字文本。我在这里介绍了一种方法,我想就改进这种方法提出批评和建议。目前,特定的应用程序将在shell脚本库中具有某种程度上是自文档化的函数。以下是我心目中的函数类型: templateFunction(){ ################################################################################ interrogationInformation=$(cat << 2012-09-26T1

我想在Bash变量中存储逐字文本。我在这里介绍了一种方法,我想就改进这种方法提出批评和建议。目前,特定的应用程序将在shell脚本库中具有某种程度上是自文档化的函数。以下是我心目中的函数类型:

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
}
templateFunction(){
################################################################################

查询信息=$(cat您可以使用
read
而不是
cat
,如下所示:

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

IFS=read-d''查询信息刚才的
queryinformation=“……”有什么问题吗
?这将需要转义许多特殊字符。虽然这当然是可能的,但在这种情况下既不方便又不清晰。用户应该能够使用库并快速编辑它。您可以看到,如果转义字符,粘贴大量XML类型的文本可能很快变得单调乏味是必需的。您只需转义美元字符(
$
)和双引号字符(
)。您还可以使用单引号(
)作为:
查询信息=“……”
(但这样您就不能使用单引号,甚至不能转义)。否则,dogbane下面的解决方案是可以的,因为它只依赖于bash的内置读取。在
bash
中还有ANSI引号字符串选项,即
$“这是一个中间嵌入了\'的字符串”
。它们的行为本质上类似于单引号字符串,但允许单引号转义(以及其他转义字符)包括在内。这些都是函数式答案,但考虑到库中可能存储了大量非shell代码,而且库中的函数都应该具有类似的形式,我认为dogbane的解决方案是最合适的。非常感谢您的输入。啊,这很好用。非常感谢您的支持救命啊!