escape single quote inside backticks

Posted on 2013-01-11
Last Modified: 2013-12-25
$mystring = "Dave's car";
`curl $mystring`;  #how to escape single quote

#I tried $string =~ s/'/\'/g; but that does not work
Question by:rgbcof
  • 3
  • 2
LVL 26

Assisted Solution

wilcoxon earned 500 total points
ID: 38767863
The problem is that $mystring contains the value Dave's car (not "Dave's car").  Either of these should fix it:

$mystring = "\"Dave's car\"";


`curl "$mystring"`;

Author Comment

ID: 38767880
(I made a  mistake on the question, here it is again)

$mystring = "Dave's car";
`curl '$mystring'`;  #how to escape single quote

#I tried $string =~ s/'/\'/g; but that does not work
LVL 31

Expert Comment

ID: 38767883
Try this:
my $string = "Dave's car";
$string =~ s/'/\\'/g;
print $string, "\n";
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.


Author Comment

ID: 38767898
that didn't work either
LVL 26

Accepted Solution

wilcoxon earned 500 total points
ID: 38767919
Unless you are using double quotes or $ characters in $mystring, changing the ' around $mystring to " on the curl line should fix the issue.

My only other idea is to try typical database escaping (which I don't think will work):

$mystring =~ s{'}{''}g; # might even need ''' - I always forget

Author Comment

ID: 38768129
$string =~ s/'/'"'"'/g;  # this work.  it's shell way to escape single quote.  Sheesh.

I guess I should have posted this question in the shell group.

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now