Extract string from brackets

后端 未结 9 1709
广开言路
广开言路 2020-12-23 16:26

I\'m pretty new at bash so this is a pretty noob question..

Suppose I have a string:

string1 [string2] string3 string4

I would like

9条回答
  •  情话喂你
    2020-12-23 16:56

    Inline solution could be:

    a="first \"Foo1\" and second \"Foo2\""
    echo ${a#*\"} | { read b; echo ${b%%\"*}; }
    

    You can test in single line:

    a="first \"Foo1\" and second \"Foo2\""; echo ${a#*\"} | { read b; echo ${b%%\"*}; }
    

    Output: Foo1

    Example with brackets:

    a="first [Foo1] and second [Foo2]"
    echo ${a#*[} | { read b; echo ${b%%]*}; }
    

    That in one line:

    a="first [Foo1] and second [Foo2]"; echo ${a#*[} | { read b; echo ${b%%]*}; }
    

    Output: Foo1

提交回复
热议问题