如何在单引号字符串中使用变量? [英] How do I use variables in single quoted strings?

查看:710
本文介绍了如何在单引号字符串中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).

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

任何帮助将不胜感激

推荐答案

变量用双引号引起来,而不用单引号引起来:

Variables are expanded in double quoted strings, but not in single quoted strings:

 $ name=World

 $ echo "Hello $name"
 Hello World

 $ echo 'Hello $name'
 Hello $name

如果您只需切换引号,就可以这样做.

If you can simply switch quotes, do so.

如果您希望坚持使用单引号来避免额外的转义,您可以改为在同一参数中混合使用引号:

If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:

 $ 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"

这篇关于如何在单引号字符串中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆