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
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 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";
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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…
The viewer will learn how to count occurrences of each item in an array.

737 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