如何在 bash 中用引号括住变量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44676356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:13:37  来源:igfitidea点击:

How to wrap quotes around variable value in bash?

bash

提问by Jyoti Dhiman

I have a variable

我有一个变量

test=value.txt

I am using test as

我正在使用测试作为

$test

which gives me

这给了我

value.txt

I want result as

我想要结果

'value.txt'

How can I achieve this?

我怎样才能做到这一点?

Edit:Above is just an example of the task I am trying to achieve. I can't change variable value.

编辑:以上只是我试图实现的任务的一个例子。我无法更改变量值。

回答by P....

Assignment using string:

使用字符串赋值:

test="'value.txt'"
echo "$test"
'value.txt'

Assignment using variable:

使用变量赋值:

var=value.txt
test="'$var'"
echo "$test"
'value.txt'

回答by hidefromkgb

Try something like this:

尝试这样的事情:

test="'value.txt'"

or this:

或这个:

test=\'value.txt\'

回答by tso

test=aaa
test="'$test'"
echo $test

output:

输出:

'aaa'