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

前端 未结 5 761
清歌不尽
清歌不尽 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

    First, you probably want quoted version of $@, i.e. "$@". To feel the difference, try putting more than one space inside the string.

    Second, quotes are element of shell's syntax -- it doesn't do you a favor. To preserve them, you need to escape them. Examples:

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

提交回复
热议问题