Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 483
user1206899 Avatar asked Aug 15 '12 08:08

user1206899


People also ask

How do you quote a single quote in bash?

I can confirm that using '\'' for a single quote inside a single-quoted string does work in Bash, and it can be explained in the same way as the "gluing" argument from earlier in the thread.

What does dollar sign do in terminal?

The system shell prompt That dollar sign means: we're in the system shell, i.e the program that you're put into as soon as you open the Terminal app. The dollar sign is often the symbol used to signify where you can begin typing in commands (you should see a blinking cursor there).

What is the difference between single and double quotation marks in bash?

If you're referring to what happens when you echo something, the single quotes will literally echo what you have between them, while the double quotes will evaluate variables between them and output the value of the variable.

What do quotation marks do in bash?

In a Bash script, when we quote a string, we set it apart and protect its literal meaning. Certain programs and utilities reinterpret or expand special characters in a quoted string. An important use of quoting is protecting a command-line parameter from the shell, but still letting the calling program expand it.


2 Answers

It causes escape sequences to be interpreted.

$ echo $'Name\tAge\nBob\t24\nMary\t36' 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.

like image 195
Ignacio Vazquez-Abrams Avatar answered Oct 10 '22 21:10

Ignacio Vazquez-Abrams


Using $ as a prefix tells BASH to try to find a variable with that name. $' is a special syntax (fully explained here) which enables ANSI-C string processing. In this case, the single tick isn't "take value verbatim until the next single tick". It should be quite safe to use. The drawbacks are it's BASH only and quite uncommon, so many people will wonder what it means.

The better way is to use single quotes. If you need a single quote in a string, you need to replace it with '\''. This ends the previous single quoted string, adds a single quote to it (\') and then starts a new single quoted string. This syntax works with any descendant of the Bourne shell, it's pretty easy to understand and most people quickly recognize the pattern.

The alternative is to replase each single tick with '"'"' which translates to "terminate current single quoted string, append double quoted string which contains just a single tick, restart single quoted string". This avoid the escape character and looks nicely symmetric. It also works the other way around if you need a double quote in a double quoted string: "'"'".

like image 23
Aaron Digulla Avatar answered Oct 10 '22 22:10

Aaron Digulla