tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Proposal to add /usr/bin/shquote



> I often find the need, in shell scripts, to escape arguments to be
> passed to other shell commands.  I'd like to add a /usr/bin/shquote
> command as a simple wrapper around shquotev(3) to do this.

> Sample usage:

>       $ shquote foo bar
>       'foo' 'bar'
>       $ set -- -foo bar "arg with spaces" "don't panic" '"Help!"'
>       $ echo "$#"
>       5
>       $ echo "$4"
>       don't panic
>       $ list="$( shquote "$@" )"
>       shquote: unknown option -- f
>       Usage: shquote [--] [arg ...]
>       $ list="$( shquote -- "$@" )"
>       $ echo "$list"
>       '-foo' 'bar' 'arg with spaces' 'don'\''t panic' '"Help!"'
>       $ eval "set -- $list"
>       $ echo "$#"
>       5
>       $ echo "$4"
>       don't panic

This discussion was more that a year ago, but...

JFYI: bash's builtin printf(1) supports %q that implements something
very close to your proposal
(%q quote the argument in a way that can be reused as shell input)

    0 cheusov>printf '%q\n' 'foo "!<>&[ ](){}; bar'
    foo\ \"\!\<\>\&\[\ \]\(\)\{\}\;\ bar
    0 cheusov>

Improved printf(1) is probably better than new very limited utility.

-- 
Best regards, Aleksey Cheusov.


Home | Main Index | Thread Index | Old Index