Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

How does the leading dollar sign affect single quotes in Bash?

I need to pass a string to a program as its argument from the Bash CLI, e.g

program "don't do this"

The string may include any character like '$', '', etc. and I don't want Bash to do any modification. So I think about using single quotes.

However the following does not work:

 program 'don't do this'            //escape doesn't work in single quote

While the following two works:

 program $'dont't do this'          //seems fine, but any other side effects?
 program 'dont'''do this'           //breaking into 3 parts

The first approach seems better in that it acquires less pre modification (put the dollar symbol in front and substitute every to \), but I don't know what else the DOLLAR SIGN might do.

I've really googled this but I can't find what I need...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It causes escape sequences to be interpreted.

$ echo $'NameAge
Bob24
Mary36'
Name    Age
Bob     24
Mary    36

After those sequences are expanded, the result is single-quoted, as if the dollar sign had not been present.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...