如何通过命令管理here-document并将结果捕获到变量中?

Mar*_*ouf 16 bash shell heredoc

现在,这将输出我在stdout上需要的值.如何将其捕获到变量中,以便我可以在脚本的其余部分中使用它?

要求:

  • 该脚本需要全部在一个文件中.
  • 如果可能的话,我宁愿不写任何临时文件.

.

#!/bin/bash

cat << EOF | xsltproc - ../pom.xml | tail -1
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:value-of select="/project/version"/></xsl:template>
</xsl:stylesheet>
EOF
Run Code Online (Sandbox Code Playgroud)

Mar*_*ouf 13

这似乎有效(基于Ignacio的答案).通过使用子shell,here-document被正确地传送到xsltproc,同时仍然通过尾部传递.

VERSION=$((xsltproc - ../pom.xml | tail -1) << EOF
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:value-of select="/project/version"/></xsl:template>
</xsl:stylesheet>
EOF
)
Run Code Online (Sandbox Code Playgroud)


Ign*_*ams 12

cat ... |是没有必要的.

foo=$(sed 's/-/_/g' << EOF
1-2
3-4
EOF
)
Run Code Online (Sandbox Code Playgroud)

  • `sed's/ - /_/g'<< EOF | 尾巴-1` (4认同)