Concatenate all arguments and wrap them with double quotes

前端 未结 6 1482
广开言路
广开言路 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:53

    ninjalj had the right idea, but the use of quotes was odd, in part because what the OP is asking for is not really the best output format for many shell tasks. Actually, I can't figure out what the intended task is, but:

    function quote_args {
       for i ; do
          echo \""$i"\"
       done
    }
    

    puts its quoted arguments one per line which is usually the best way to feed other programs. You do get output in a form you didn't ask for:

    $ quote_args this is\ a "test really"
    "this"
    "is a"
    "test really"
    

    but it can be easily converted and this is the idiom that most shell invocations would prefer:

    $ echo `quote_args this is\ a "test really"`
    "this" "is a" "test really"
    

    but unless it is going through another eval pass, the extra quotes will probably screw things up. That is, ls "is a file" will list the file is a file while

    $ ls `quote_args is\ a\ file`
    

    will try to list "is, a, and file" which you probably don't want.

提交回复
热议问题