What is the most aesthetic way to to escape a single quote within a single-quoted string in (ba)sh?

前端 未结 5 1330
予麋鹿
予麋鹿 2021-01-26 06:23

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

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

This is a syntax error, since

5条回答
  •  -上瘾入骨i
    2021-01-26 07:20

    I'm not sure why switching to $'...' is an option but switching to double quotes is not (presumably to prevent some sort of parameter expansion). But you don't need to double quote the entire string:

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

提交回复
热议问题