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";
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.


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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will giveā€¦
The viewer will learn how to dynamically set the form action using jQuery.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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