简体   繁体   English

Bash:有什么方法可以在单引号括起的字符串中打印变量?

[英]Bash: Any way to print a variable inside a string surrounded by single quotes?

I have a variable $NEWFILE which i want to print inside of a string surrounded by single quotes like this我有一个变量 $NEWFILE,我想在一个被单引号包围的字符串中打印出来,就像这样

cp '/path/to/file/$NEWFILE' /path/to/another/file

$NEWFILE can be variable like sunny fields.jpg . $NEWFILE 可以像sunny fields.jpg一样可变。 Basically I want the final output to be基本上我希望最后的 output 是

cp '/path/to/file/sunny fields.jpg' /path/to/another/file

How do I go about this?我怎么go一下这个? I tried to put the whole string inside double quotes我试图将整个字符串放在双引号内

cp "'/path/to/file/$NEWFILE'" /path/to/another/file

for $NEWFILE = 'sunny fields.jpg'.对于 $NEWFILE = 'sunny fields.jpg'。 But it didn't work as expected但它没有按预期工作

single quotes don't allow, by default, expand variables (feature that you need), so if you just replace it's supposed to works (it worked in my test):默认情况下,单引号不允许扩展变量(您需要的功能),因此如果您只是替换它应该可以工作(它在我的测试中工作):

cp "/path/to/file/$NEWFILE" /path/to/another/file

I just removed your single quotes.我刚刚删除了你的单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM