LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 06-15-2010, 06:21 AM   #1
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Rep: Reputation: 15
Thumbs down How to remove a certain character from a variable


Hi

I want to remove single quote from a variable.


eg: Suppose I have a variable VAR='TEST' (including single quotes)

How can I remove single quotes and make it TEST.

Thanks in advance for your valuable help...
 
Old 06-15-2010, 06:34 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Code:
[mherring@mystical ~]$ var="'test'"
[mherring@mystical ~]$ echo $var
'test'
[mherring@mystical ~]$ echo $var|sed "s/'//g"
test
 
Old 06-15-2010, 06:37 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Or using (a bit more cryptic) parameter substitution:
Code:
$ echo ${var//\'/}
test
 
Old 06-15-2010, 06:57 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Here's another, just so you have a variety of methods to choose from. Probably the example above by colucix is the most efficient, since it is wholly done by the shell (no external tools), but this one here uses `tr` in the same manner as Pixellany's code uses `sed`; `tr` is a little faster than `sed` though, so if you had to do a million of this, you might notice `tr` saves a little time:

Code:
echo "'test'" | tr -d \'
test
 
Old 06-15-2010, 08:49 AM   #5
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks....
 
Old 06-15-2010, 09:03 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Don't forget to mark as SOLVED when you have your solution.
 
  


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
remove first and last character zeroquaranta Programming 6 02-25-2010 03:10 AM
insert character into a variable jadeddog Programming 4 11-04-2008 04:39 PM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 11:11 AM
python: using a variable to print unicode character donnied Programming 3 02-09-2008 03:51 AM
[Bash] New line character in variable michael_hk Linux - Newbie 4 06-15-2006 03:13 PM


All times are GMT -5. The time now is 12:58 AM.

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