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

后端 未结 15 1728
刺人心
刺人心 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.

    0 讨论(0)
  • 2020-11-30 17:39

    There is another way to do it. Like:

    echo ${opt:1:-1}
    
    0 讨论(0)
  • 2020-11-30 17:40

    If you try to remove quotes because the Makefile keeps them, try this:

    $(subst $\",,$(YOUR_VARIABLE))
    

    Based on another answer: https://stackoverflow.com/a/10430975/10452175

    0 讨论(0)
提交回复
热议问题