How to preserve double quotes in $@ in a shell script?

前端 未结 5 763
清歌不尽
清歌不尽 2020-12-10 13:59

Let\'s say I have a really simple shell script \'foo\':

  #!/bin/sh
  echo $@

If I invoke it like so:

  foo 1 2 3
         


        
5条回答
  •  难免孤独
    2020-12-10 14:29

    You need to quote the quotes:

    foo 1 "\"this arg has whitespace\"" 3
    

    or (more simply)

    foo 1 '"this arg has whitespace"' 3
    

    You need to quote the double quotes to make sure that the shell doesn't remove them when parsing word arguments.

提交回复
热议问题