Concatenate all arguments and wrap them with double quotes

前端 未结 6 1471
广开言路
广开言路 2020-12-15 05:52
function foo() {
A=$@...
echo $A
}

foo bla \"hello ppl\"

I would like the output to be:
\"bla\" \"hello ppl\"

What do I need to do in

6条回答
  •  死守一世寂寞
    2020-12-15 06:26

    No loop required:

    foo() { local saveIFS=$IFS; local IFS='"'; local a="${*/#/ \"}"; echo "$a"\"; IFS=$saveIFS; }
    

    Saving IFS isn't necessary in this simple example, especially restoring it right before the function exits, due to the fact that local is used. However, I included it in case other things are going into the function that a changed IFS might affect.

    Example run:

    $ foo a bcd "efg hij" k "lll mmm nnn " ooo "   ppp   qqq   " rrr\ sss
     "a" "bcd" "efg hij" "k" "lll mmm nnn " "ooo" "   ppp   qqq   " "rrr sss"
    

提交回复
热议问题