This tutorial will help you to remove start and ending double quote from string in shell script. Where the string is stored in a variable.
Remove Double Quote from a String
The sed command line utility helps to easily handle this. A single line sed command can remove quotes from start and end of the string.
sed -e 's/^"//' -e 's/"$//' <<<"$var1"
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.
Remove Double Quote and Store Output
The result will be printed on the terminal. You can also save the result to variable and or redirect output to a file.
The below commands will help you to remove double quote and store output to the same or different variable.
var2=`sed -e 's/^"//' -e 's/"$//' <<<"$var1"`
#Save in another variable
var1=`sed -e 's/^"//' -e 's/"$//' <<<"$var1"`
#Save in same variable
Even you can store the result in a file. like:
sed -e 's/^"//' -e 's/"$//' <<<"$var1" > out_var.txt
This tutorial helped you to remove start and ending double quotes from a string stored in a variable using shell script.