如何在单引号字符串中使用变量?

如何在单引号字符串中使用变量?

我只是想知道如何在单引号内回显变量(我使用单引号,因为字符串中有引号)。

     echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}

任何帮助将不胜感激



千万里不及你
浏览 951回答 3
3回答

呼唤远方

变量以双引号字符串扩展,但不在单引号字符串中扩展: $ name=World  $ echo "Hello $name"  Hello World  $ echo 'Hello $name'  Hello $name如果您只需切换引号,请执行此操作。如果您更喜欢使用单引号来避免额外的转义,则可以在同一参数中混合和匹配引号: $ echo 'single quoted. '"Double quoted. "'Single quoted again.'  single quoted. Double quoted. Single quoted again.  $ echo '"$name" has the value '"$name"  "$name" has the value World适用于您的案例: echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"

慕码人2483693

使用printf:printf 'test text "here_is_some_test_text_%s" "output"\n' "$counter" >> ${FILE}

慕姐4208626

使用heredoc:cat&nbsp;<<&nbsp;EOF&nbsp;>>&nbsp;${FILE}test&nbsp;text&nbsp;"here_is_some_test_text_$counter"&nbsp;"output"EOF
打开App,查看更多内容
随时随地看视频慕课网APP