When the shell encounters a string between backquotes
`cmd`
it executes cmd and replaces the backquoted string with the standard output of cmd, with any trailing newlines deleted. Quoting inside backquoted commands is somewhat complicated, mainy because the same token is used to start and to end a backquoted command. As a consequence, to nest backquoted commands, the backquotes of the inner one have to be escaped using backslashes, e.g.
LABNAME=$(echo $labname | tr "[a-z]" "[A-Z]" | sed 's/-/_/g'
cat <<-IOTA >$iota_cfg
path=\`checkpath\`
export ${LABNAME}_COOLXDIR=$COOLXDIR
IOTA
Since the LABNAME is a variable defined out of the here document, it must be quoted by braces {}, or error will occur.
--------------
Reference:
本文深入探讨了Shell中使用backquotes执行命令并替换字符串的过程,包括内部命令引用、变量作用及错误避免策略,同时展示了如何在heredoc中安全地使用变量。
2895

被折叠的 条评论
为什么被折叠?



