LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to remove a certain character from a variable (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-remove-a-certain-character-from-a-variable-814228/)

linuxromeo 06-15-2010 05:21 AM

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...

pixellany 06-15-2010 05:34 AM

Code:

[mherring@mystical ~]$ var="'test'"
[mherring@mystical ~]$ echo $var
'test'
[mherring@mystical ~]$ echo $var|sed "s/'//g"
test


colucix 06-15-2010 05:37 AM

Or using (a bit more cryptic) parameter substitution:
Code:

$ echo ${var//\'/}
test


GrapefruiTgirl 06-15-2010 05:57 AM

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


linuxromeo 06-15-2010 07:49 AM

Thanks....

grail 06-15-2010 08:03 AM

Don't forget to mark as SOLVED when you have your solution.


All times are GMT -5. The time now is 08:45 PM.