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

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

    What i'd do is to quote all the arguments received with spaces that might help your case.

    for x in "${@}" ; do
        # try to figure out if quoting was required for the $x
        if [[ "$x" != "${x%[[:space:]]*}" ]]; then
            x="\""$x"\""
        fi
        echo $x
        _args=$_args" "$x
    done
    
    echo "All Cmd Args are: $_args"
    

提交回复
热议问题