POSIX sh equivalent for Bash’s printf %q

前端 未结 4 1503
我寻月下人不归
我寻月下人不归 2020-12-05 05:36

Suppose I have a #!/bin/sh script which can take a variety of positional parameters, some of which may include spaces, either/both kinds of quotes, etc. I want

4条回答
  •  不思量自难忘°
    2020-12-05 05:46

    The following seems to work with everything I have thrown at it so far, including spaces, both kinds of quotes and a variety of other metacharacters, and embedded newlines:

    #!/bin/sh
    quote() {
        echo "$1" | sed "s/'/'\"'\"'/g"
    }
    args=
    for arg in "$@"
    do
        argq="'"`quote "$arg"`"'"
        args="$argq $args"
    done
    eval "ls $args"
    

提交回复
热议问题