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

user1263746 picture user1263746 · Mar 16, 2012 · Viewed 284.7k times · Source

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

Answer

wieczorek1990 picture wieczorek1990 · Oct 11, 2014

Use tr to delete ":

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

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