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

后端 未结 15 1779
刺人心
刺人心 2020-11-30 16:47

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?

15条回答
  •  感情败类
    2020-11-30 17:38

    The easiest solution in Bash:

    $ s='"abc"'
    $ echo $s
    "abc"
    $ echo "${s:1:-1}"
    abc
    

    This is called substring expansion (see Gnu Bash Manual and search for ${parameter:offset:length}). In this example it takes the substring from s starting at position 1 and ending at the second last position. This is due to the fact that if length is a negative value it is interpreted as a backwards running offset from the end of parameter.

提交回复
热议问题