Shell script - remove first and last quote () from a variable
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).
Stripping single and double quotes in a string using bash / standard Linux commands only
This should do it:
sed "s/^\([\"']\)\(.*\)\1\$/\2/g" in.txt
Where in.txt is:
"Fo'od'
'Food'
"Food"
"Fo"od'
Food
'Food"
"Food'
'Fo'od'
"Fo'od"
Fo'od
'Fo"od'
"Fo"od"
Fo"od
And expected.txt is:
"Fo'od'
Food
Food
"Fo"od'
Food
'Food"
"Food'
Fo'od
Fo'od
Fo'od
Fo"od
Fo"od
Fo"od
You can check they match with:
diff -s <(sed "s/^\([\"']\)\(.*\)\1\$/\2/g" in.txt) expected.txt
How to escape single quotes within single quoted strings
If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:
alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234
Explanation of how '"'"'
is interpreted as just '
:
'
End first quotation which uses single quotes."
Start second quotation, using double-quotes.'
Quoted character."
End second quotation, using double-quotes.'
Start third quotation, using single quotes.
If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.
UNIX Bash - Removing double quotes from specific strings within a file
This might work for you (GNU sed):
sed 's/\."\([^"]*\)"/.\1/g' file
Match on a period, followed by a double quoted string(s) and replace by a period and the string less the double quotes.
N.B. The period needs to be quoted/escaped otherwise it matches any character.
How do I escape slashes and double and single quotes in sed?
The s///
command in sed
allows you to use other characters instead of /
as the delimiter, as in
sed 's#"http://www\.fubar\.com"#URL_FUBAR#g'
or
sed 's,"http://www\.fubar\.com",URL_FUBAR,g'
The double quotes are not a problem. For matching single quotes, switch the two types of quotes around. Note that a single quoted string may not contain single quotes (not even escaped ones).
The dots need to be escaped if sed
is to interpret them as literal dots and not as the regular expression pattern .
which matches any one character.
Related Topics
Why Is There No Directx API for Linux
How to Determine If Code Is Running in Signal-Handler Context
Using Assertion in the Linux Kernel
How to Get a List of All Valid Ip Addresses in a Local Network
How to Simulate a Failed Disk During Testing
Execute Shell Commands from Program Running in Wine
How to Convert ".." in Path Names to Absolute Name in a Bash Script
Saving Gmon.Out Before Killing a Process
Centos Error - Sudo: Effective Uid Is Not 0, Is Sudo Installed Setuid Root
How to Add Date String to Each Line of a Continuously Written Log File
Cannot Install Docker in a Rhel Server
Percentage Value with Gnu Diff
Convert Utf8 to Utf16 Using Iconv
How to Run Dos2Unix on an Entire Directory
How Can the Linux Kernel Be Forced to Enumerate the Pci-E Bus