How to Use Single Quote Inside an Echo Which Is Using Single Quote

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.

How do I use single quotes inside single quotes?

echo "<div id=\"panel1-under\">Welcome ".$_SESSION['username']."</div>";

or

echo '<div id="panel1-under">Welcome '.$_SESSION['username'].'</div>';

Quick Explain :

  • You don't have to reopen the tags inside a echo String (" ... ")
  • What I have done here is to pass the string "Welcome " concatenated to $_SESSION['username'] and "" (what the . operator does)
  • PHP is even smart enough to detect variables inside a PHP string and evaluate them :

    $variablename = "Andrew";

    echo "Hello $variablename, welcome ";

=> Hello Andrew, welcome

More infos : PHP.net - echo

How to use single quote inside an echo which is using single quote

Either escape the quote with a backslash, or use double quotes to designate the string.

echo 'Here goes your message with an apostrophe S like thi\'s';

echo "Here goes your message with an apostrophe S like thi's";

How to echo single quotes from within shell in find exec

Assign a quote to a temp variable (I also remove the path).

q="'" find . -iname "*" -exec sh -c 'echo "file $q${0##*/}$q"' {} \;

How to work with double quotes and single quotes in php

You have to escape double quotes if your string is put in double quotes by using backslash \ character. Here is an example:

echo "String with \"double quotes\"";

So in your case it will be:

echo "<img src=\"img_fjords.jpg\" onclick=\"document.getElementById('someID').style.display='block'\" class=\"w3-hover-opacity\"";

Ref: Double quotes within php script echo

I want to embed a single quote in a string

As tripleee points out in comments on the question, the best approach in this particular scenario is to use a double-quoted string, in which you can embed both variable references (e.g., $i) and single quotes as-is; e.g.: z="'$i.ala.r$x.sph $i .ala.r$y.sph'"
This answer focuses on the various approaches to producing / embedding literal ' chars. in strings, starting with the OP's misconception.

Your use of '\'' suggests that you're confused by the workaround that is commonly used to "embed" a single quote in an overall single-quoted string, which is not what your code does on the z=... line, because it starts with '\''.

If we simplify your command, we get:

echo '\''$i

which is a syntax error, because to Bash the single quotes are unbalanced, because '\' by itself is considered a complete single-quoted string containing literal \, followed by the opening ' of a second single-quoted string, which is never closed.

Again it's worth noting that "'$i" is the best solution to this specific problem: the ' can be embedded as-is, and including variable reference $i inside the double-quoted string protects its value from potentially unwanted word-splitting and filename expansion (globbing).

POSIX-like shells provide NO way to embed single quotes inside a single-quoted string - not even with escaping. Hence, the \ in '\' is simply treated as a literal (see below for a workaround).

The rest of this answer shows all approaches to producing a literal ', both inside and outside quoted strings.


To create a single quotation mark outside of a quoted string, simply use \':

$ echo I am 6\' tall.
I am 6' tall.

This quotes (escapes) the individual ' character only, using \.
But note that tokens placed outside the context of a single- or double-quoted string on a command line are subject to word-splitting and filename expansion (globbing).


To use a single quote inside a double-quoted string, use it as-is (no escaping needed):

$ echo "I am 6' tall."
I am 6' tall.

This is the best choice if you also want to embed variable references (e.g., $i) or commands (via command substitutions, $(...)) in your string (you can suppress interpolation by escaping $ as \$).


To use a single quote inside a single-quoted string (in which no interpolations (expansions) are performed by design), you must use a workaround:

$ echo 'I am 6'\'' tall.'
I am 6' tall.

The workaround is necessitated by single-quoted strings not supporting embedded single quotes at all; the '\'' part only makes sense "inside" a single-quoted string in that:

  • the leading ' terminates the single-quoted string so far
  • the \' then produces a ' literal individually escaped with \ outside the context of a quoted string.
  • the trailing ' then "restarts" the remainder of the single-quoted string.

In other words: While you cannot directly embed a single quote, you can break the single-quoted string into multiple pieces, insert individually \-escaped ' instances outside the single-quoted string as needed, and let Bash's string concatenation (which automatically joins directly adjacent string) piece it all back together to form a single string.


chepner points out in a comment that you can alternatively use a here-document with a quoted opening delimiter, which acts like a single-quoted string while allowing embedding of ' chars:

read -r var <<'EOF' # quoted delimiter -> like a '...' string, but ' can be embedded
I am 6' tall.
EOF

With an unquoted opening delimiter, the here-document acts like a double-quoted string, which, just like the latter, also allows embedding ', while also supporting expansions:

read -r var <<EOF # unquoted delimiter -> like a "..." string
$USER is 6' tall.
EOF

Finally, if remaining POSIX-compliant is not a must, you can use an ANSI C-quoted string string, which allows embedding single quotes with \';

note that such strings interpret control-character escape sequences such as \n, but otherwise, like a normal single-quoted string, do not perform interpolation of variable references or command substitutions:

$ echo $'I am 6\' tall.'
I am 6' tall.

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.

How to escape a single quote in single quote string in Bash?

echo 'I\'m a student'

does not work. But the following works:

echo $'I\'m a student'

From the man page of bash:

A single quote may not occur between single quotes, even when preceded
by a backslash.

....

Words of the form $'string' are treated specially. The word
expands to string, with backslash-escaped characters replaced as
specified by the ANSI C standard.



Related Topics



Leave a reply



Submit