bash 在 shell 编程中打印双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18929149/
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
print double quotes in shell programming
提问by Nagaraju
I want to print double quotes using echo statement in shell programming.
我想在 shell 编程中使用 echo 语句打印双引号。
Example:
示例:
echo ",,,";
prints xyz,123,abc,pqrs
印刷 xyz,123,abc,pqrs
How to print "xyz","123","abc","pqrs";
如何打印 "xyz","123","abc","pqrs";
I had tried to place double quotes in echo statement but its not being printed.
我曾试图在 echo 语句中放置双引号,但它没有被打印出来。
回答by konsolebox
You just have to quote them:
你只需要引用它们:
echo "\"\",\"\",\"\",\"\""
As noted here:
正如指出的在这里:
Enclosing characters in double quotes (‘"') preserves the literal value of all characters within the quotes, with the exception of ‘$', ‘`', ‘\', and, when history expansion is enabled, ‘!'. The characters ‘$' and ‘`' retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: ‘$', ‘`', ‘"', ‘\', or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ‘!' appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!' is not removed.
The special parameters ‘*' and ‘@' have special meaning when in double quotes (see Shell Parameter Expansion).
用双引号 ('"') 将字符括起来会保留引号内所有字符的字面值,但 '$'、'`'、'\' 和启用历史扩展时的 '!' 除外。字符 '$' 和 '`' 在双引号内保留其特殊含义(参见 Shell Expansions)。反斜杠仅在后跟以下字符之一时保留其特殊含义:'$'、'`'、'"'、' \' 或换行符。在双引号内,后跟这些字符之一的反斜杠将被删除。没有特殊含义的字符前面的反斜杠保持不变。双引号可以通过在双引号前面加上反斜杠来引用。如果启用,将执行历史扩展,除非'!' 出现在双引号中的是使用反斜杠进行转义。'!' 前面的反斜杠 没有被移除。
特殊参数 '*' 和 '@' 在双引号中具有特殊含义(请参阅 Shell 参数扩展)。
回答by devnull
Use printf
, no escaping is required:
使用printf
,不需要转义:
printf '"%s","%s","%s","%s";\n'
and the trailing ;
gets printed too!
并且尾随也;
被打印出来!
回答by Ashish Gaur
You should escape the "
to make it visible in the output, you can do this :
您应该转义"
以使其在输出中可见,您可以这样做:
echo \"""\",\"""\",\"""\",\"""\"