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

前端 未结 5 1340
予麋鹿
予麋鹿 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条回答
  •  面向向阳花
    2021-01-26 06:58

    A non-POSIX solution which is supported in bash, at least, is the following:

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

    The $ causes escape processing.

提交回复
热议问题