tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: Proposal to add /usr/bin/shquote
On Fri, Sep 05, 2008 at 09:38:52PM +0200, Alan Barrett wrote:
> 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
>
> --apb (Alan Barrett)
I don't see the problem ?
You just need to understand when the shell does 'field splitting'.
Try running:
show_args() {
name="$1"
shift
echo $name: $# args
while [ -n "$1" ]
do
echo " $1"
shift
done
}
set -- -foo bar "arg with spaces" "don't panic" '"Help!"'
echo "$#"
echo "$4"
show_args '$*' $*
show_args '"$*"' "$*"
show_args '$@' $@
show_args '"$@"' "$@"
show_args '$1-$4' $1 $2 $3 $4
show_args '"$1"-"$4"' "$1" "$2" "$3" "$4"
(IFS=; show_args '$1-$4 (no IFS)' $1 $2 $3 $4)
In general shell scripts ought to put most $foo expansions inside "".
David
--
David Laight: david%l8s.co.uk@localhost
Home |
Main Index |
Thread Index |
Old Index