在(ba)sh中用单引号引起来的单引号转义的最美学方法是什么? [英] What is the most aesthetic way to to escape a single quote within a single-quoted string in (ba)sh?

查看:104
本文介绍了在(ba)sh中用单引号引起来的单引号转义的最美学方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的(ba)sh脚本中,例如:

In a (ba)sh script of mine, I have, for example:

MYVAR='Alice says: "Hello, Bob." But Bob isn't listening.'

这是语法错误,因为isn't中的'结束单引号字符串.我知道我可以使用

This is a syntax error, since the ' in isn't ends the single-quoted string. I know I can fix this using

MYVAR='Alice says: "Hello, Bob." But Bob isn'"'"'t listening.'

但是那太丑了...我该怎么办呢? sh不支持

But that is sooo ugly... what can I do instead? sh doesn't support

MYVAR='Alice says: "Hello, Bob." But Bob isn\'t listening.'

那是可以容忍的,切换到双引号字符串对我来说不是一个选择.

Which would have been tolerable, and switching to a double-quotes string is not an option for me.

推荐答案

不,您不能在Bash(或korn shell)的单个带引号的字符串中嵌入单个引号.这是您选择的语言的功能.在单引号内没有特殊字符,其中包括\.

No, you can't embed a single quote inside a single quoted string in Bash (or korn shell). It is a feature of the language you have chosen. Inside single quotes there are no special characters, and that includes \.

这是您的一种解决方案的变体,很丑陋:

This is a variation on one of your solutions, and it is ugly:

 echo 'Alice says: "Hello, Bob." But Bob isn'\''t listening.'

给予:

Alice says: "Hello, Bob." But Bob isn't listening.

或者使用其他语言,例如Perl或Python.

Alternatively use a different language, like Perl or Python.

这篇关于在(ba)sh中用单引号引起来的单引号转义的最美学方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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