Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script - remove first and last quote (") from a variable

Below is the snippet of a shell script from a larger script. It removes the quotes from the string that is held by a variable. I am doing it using sed, but is it efficient? If not, then what is the efficient way?

#!/bin/sh  opt="\"html\\test\\\"" temp=`echo $opt | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/'` echo $temp 
like image 781
user1263746 Avatar asked Mar 16 '12 07:03

user1263746


People also ask

How do you remove a quote from a variable in shell?

${temp#\"} will remove the prefix " (escaped with a backslash to prevent shell interpretation). Another advantage is that it will remove surrounding quotes only if there are surrounding quotes.

How do I remove the first and last quotes from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you remove a quote from a variable?

A single line sed command can remove quotes from start and end of the string. The above sed command execute two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string.

How do I get rid of double quotes in bash?

Single quotes(') and backslash(\) are used to escape double quotes in bash shell script. We all know that inside single quotes, all special characters are ignored by the shell, so you can use double quotes inside it. You can also use a backslash to escape double quotes.


2 Answers

Use tr to delete ":

 echo "$opt" | tr -d '"' 

Note: This removes all double quotes, not just leading and trailing.

like image 92
wieczorek1990 Avatar answered Sep 24 '22 15:09

wieczorek1990


There's a simpler and more efficient way, using the native shell prefix/suffix removal feature:

temp="${opt%\"}" temp="${temp#\"}" echo "$temp" 

${opt%\"} will remove the suffix " (escaped with a backslash to prevent shell interpretation).

${temp#\"} will remove the prefix " (escaped with a backslash to prevent shell interpretation).

Another advantage is that it will remove surrounding quotes only if there are surrounding quotes.

BTW, your solution always removes the first and last character, whatever they may be (of course, I'm sure you know your data, but it's always better to be sure of what you're removing).

Using sed:

echo "$opt" | sed -e 's/^"//' -e 's/"$//' 

(Improved version, as indicated by jfgagne, getting rid of echo)

sed -e 's/^"//' -e 's/"$//' <<<"$opt" 

So it replaces a leading " with nothing, and a trailing " with nothing too. In the same invocation (there isn't any need to pipe and start another sed. Using -e you can have multiple text processing).

like image 42
huelbois Avatar answered Sep 23 '22 15:09

huelbois