Interpolation Within Single Quotes

Interpolation within single quotes

You can use %{text contains "#{search.query}"} if you don't want to escape the double quotes "text contains \"#{search.query}\"".

Single quote string string interpolation

You cannot use string interpolation with single-quoted strings in Ruby.

But double-quoted strings can!

from = "'Name of Person' <#{ENV['EMAIL']}>"

But if you want to keep your double-quotes wrapping the Name of Person, you can escape them with a backslash \:

from = "\"Name of Person\" <#{ENV['EMAIL']}>"

Or use string concatenation:

from = '"Name of Person" <' + ENV['EMAIL'] + '>'
# but I find it ugly

Why does single quote not work with string interpolation in ruby

Ruby doesn't interpret single-quoted strings.

This might seem like a limitation at first, but it's actually a nice feature. It allows you to enter many characters without having to escape them, which results in more legible code:

file = 'C:\foo\bar\baz.txt'

# as opposed to:

file = "C:\\foo\\bar\\baz.txt"

Or when having a string about string interpolation itself: (note that Stack Overflow's syntax highlighting is misleading – there's no interpolation)

string = 'In Ruby, you can write "1 + 2 = #{ 1 + 2 }" to get "1 + 2 = 3".'

# instead of:

string = "In Ruby, you can write \"1 + 2 = \#{ 1 + 2 }\" to get \"1 + 2 = 3\"."

Apart from '...' and "...", Ruby also has %q(...) and %Q(...) style string literals (the former without, the latter with interpolation). This is especially useful if your string contains both, single and double quotes:

string = %q(A string containing '...' and "...")

You can even pick your own delimiter: (again, the syntax highlighter can't keep up)

string = %q@A string containing '...', "..." and (...)"@

And finally, you can mix and match different string literal styles:

string = %q(foo) 'bar' "baz"
#=> "foobarbaz"

Why Does String Interpolation Only Work In Double Quotes PHP

The obvious answer is that there are times when you might not want variables to be interpreted in your strings.

Take, for example, the following line of code:

$currency = "$USD";

This produces an "undefined variable" notice and $currency is an empty string. Definitely not what you want.

You could escape it ("\$USD"), but hey, that's a faff.

So PHP, as a design decision, chose to have double-quoted strings interpolated and single-quoted strings not.

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

Ruby's string interpolation seems to behave differently with single quotes vs. double quotes

Yes, the answer is that you cannot put variables in single quoted strings, eg string interpolation is not allowed in single quoted strings. Further, you might be interested, escape sequences do not work in single quoted strings (Except for escaping single quotes themselves like 'don\'t').

There is also some debate about whether or not there's performance benefits of single vs double quotes, but I'm not seeing any convincing cases.

C# How to wrap date string in single quote inside the string's double quotes

Use string.Format, or on C#6+ string interpolation:

// String format
string.Format("'{0}'", DateTime.Now.ToString("yyyy-MM-dd"));

// Interpolation
$"'{DateTime.Now.ToString("yyyy-MM-dd")}'";

Result:

'2017-03-30'

Double vs single quotes

" " allows you to do string interpolation, e.g.:

world_type = 'Mars'
"Hello #{world_type}"

bash: How to warp interpolated variable in quotes to pass it (together with quotes) as argument to another command in script

The argument that the docker binary sees will contain only the double quotes.

The single quotes are added by the set -x mechanism only for the purpose of showing you what is in the argument list.


I suspect you don't actually want docker to see any of the quotes. In that case, don't escape them in your command, and instead do

docker run -e "$env" some_image

Double quotes in that position means that if the value of $env contains spaces, it will still be passed to docker as a single argument (with spaces in it), rather than being split into several argument (which would confuse it).

These two commands will produce exactly the same argument array in the new process:

docker run -e VAR=val some_image
docker run -e "VAR=val" some_image

In the second of these cases the double quotes are interpreted by the shell (even though they are not necessary because the argument doesn't contain spaces), and what docker sees in both cases is just the string VAR=val.

If you try something like

env="VAR=multiple words"
set +x
docker run -r "$env" some_image

the third command-line argument to docker will be the spaceful string VAR=multiple words, and this get printed by set -x as something like

+ docker run -r 'VAR=multiple words' some_image

because the print-the-command-line-before-executing code can see it would be confusing not to quote this spaceful string. Again the docker command doesn't see any quotes at all in this invocation.


For debugging these things it can be useful to replace the actual command you're starting with echo. This will print the raw arguments the command sees, without adding any quoting of its own. The downside of this is that you won't be able to tell the difference between a space that is part of a single argument and the space that echo prints by itself between the arguments.

Expansion of variables inside single quotes in a command in Bash

Inside single quotes everything is preserved literally, without exception.

That means you have to close the quotes, insert something, and then re-enter again.

'before'"$variable"'after'
'before'"'"'after'
'before'\''after'

Word concatenation is simply done by juxtaposition. As you can verify, each of the above lines is a single word to the shell. Quotes (single or double quotes, depending on the situation) don't isolate words. They are only used to disable interpretation of various special characters, like whitespace, $, ;... For a good tutorial on quoting see Mark Reed's answer. Also relevant: Which characters need to be escaped in bash?

Do not concatenate strings interpreted by a shell

You should absolutely avoid building shell commands by concatenating variables. This is a bad idea similar to concatenation of SQL fragments (SQL injection!).

Usually it is possible to have placeholders in the command, and to supply the command together with variables so that the callee can receive them from the invocation arguments list.

For example, the following is very unsafe. DON'T DO THIS

script="echo \"Argument 1 is: $myvar\""
/bin/sh -c "$script"

If the contents of $myvar is untrusted, here is an exploit:

myvar='foo"; echo "you were hacked'

Instead of the above invocation, use positional arguments. The following invocation is better -- it's not exploitable:

script='echo "arg 1 is: $1"'
/bin/sh -c "$script" -- "$myvar"

Note the use of single ticks in the assignment to script, which means that it's taken literally, without variable expansion or any other form of interpretation.



Related Topics



Leave a reply



Submit