LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2016, 08:57 AM   #1
karthika.s
LQ Newbie
 
Registered: May 2016
Posts: 1

Rep: Reputation: Disabled
In a string how to add backslash "\" before apostrophe


Iam creating a shell script that have a variable with apostrophe. Want to add backslash before the variable like "St Antony\'s company" and give this variable in the WHERE condition of a sql query.

My script

Var1="St Antony's company"

if [[ "$Var1" =~ \' ]]
then
Var1= `echo $Var1 | sed s/\'/\\\\\'/g`
echo $Var1
fi

echo -n "SELECT field1, field2 FROM table1 WHERE field3='$VAR'";

but it is not working. Please help me
 
Old 05-12-2016, 09:10 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Use double quotes around sed command
Code:
sed "s/'/\\\'/g"
Maybe prepared statements could be a nice option though (protect against sql injection also)

Last edited by keefaz; 05-12-2016 at 09:13 AM.
 
Old 05-12-2016, 09:15 AM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Quote:
Originally Posted by keefaz View Post
Maybe prepared statements could be a nice option though (protect against sql injection also)
Prepared statements help with a lot, especially against injection attacks.
 
Old 05-12-2016, 10:09 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
If using bash you could just do the change in place:
Code:
Var1="St Antony's company"

echo -n "SELECT field1, field2 FROM table1 WHERE field3='${VAR//\'/\\\'}'";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Latex Problem: Backslash Recognized as Two Backslashes "doesn't match its definition" rm_-rf_windows Linux - General 2 05-09-2012 09:54 PM
[SOLVED] Minor grammar point - missing apostrophe in "Post New Thread" Snark1994 LQ Suggestions & Feedback 1 07-13-2011 08:04 AM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
Accepting single backslash "\" as a user input. sundar_r05 Linux - Enterprise 6 02-02-2010 05:29 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration