Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

echo

quotes

First of all, i have gone through the related questions.. haven't found any answer.. I m using this code to display a message

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

How can i make this work, as any quote inside this echo will break the statement...

like image 246
Nasir Zia Avatar asked Aug 03 '12 09:08

Nasir Zia


People also ask

How do you use single quotes inside quotes?

' End first quotation which uses single quotes. " Start second quotation, using double-quotes. ' Quoted character. " End second quotation, using double-quotes.

How do you handle a single quote in a string?

A single quote is not used where there is already a quoted string. So you can overcome this issue by using a backslash following the single quote. Here the backslash and a quote are used in the “don't” word. The whole string is accompanied by the '$' sign at the start of the declaration of the variable.

How do you write a single quote in a shell script?

The Single QuotesCharacters within single quotes are quoted just as if a backslash is in front of each character. With this, the echo command displays in a proper way.


2 Answers

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";
like image 100
Madara's Ghost Avatar answered Sep 18 '22 05:09

Madara's Ghost


Escape the quote using a backslash.

'hello\'s'

The single quote that appears after the backslash will appear on screen.

like image 36
He Hui Avatar answered Sep 21 '22 05:09

He Hui