escape single quote inside backticks

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

#I tried $string =~ s/'/\'/g; but that does not work
Question by:rgbcof
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 26

Assisted Solution

wilcoxon earned 1000 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" http:myservice.com`;

Author Comment

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

$mystring = "Dave's car";
`curl '$mystring' http:myservice.com`;  #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";
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.


Author Comment

ID: 38767898
that didn't work either
LVL 26

Accepted Solution

wilcoxon earned 1000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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